先启动mongo
连接好mongodb后用pycharm链接
这里需要在终端
pip install pymongo
from pymongo import MongoClient
# mongod --dbpath C:\Users\HP\Desktop\mogodb\data
client = MongoClient("127.0.0.1:27017")
db = client['primer']
coll = db['colletion2']
doc = {'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}
docs = [
{'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'},
{'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'},
{'item': 'paper', 'qty': 100.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'},
{'item': 'planner', 'qty': 75.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'},
{'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}
]
result = coll.insert_one(doc)
result =coll.insert_many(docs)
#运行结束后不可二次运行此代码
#showall
cursor = coll.find()
#单个输出
print("所有数据逐个输出")
for d in cursor:
print(d)
print("所有数据")
print(list(coll.find()))
#单个输出函数
def dfp(in_cursor):
for d in in_cursor:
print(d)
return "------------------------------------------"
# 条件查询
cursor1 = coll.find({"qty":25}) ##注意qty在mongodb shell中可以不带双引号,在Python中不行
cursor2 = coll.find({"qty":{"$gte":45}})
cursor3 = coll.find({"$or":[{"qty":{"$gte":45}}, {"status":'A'}]})
cursor4 = coll.find({"size.h":10})
print("条件查询")
print(list(cursor1))
print(list(cursor2))
print(list(cursor3))
print(list(cursor4))
# 投影
cursor = coll.find({},{"item":1})
print("投影")
print(dfp(cursor))
# 分页
cursor = coll.find().skip(2).limit(2)
print("分页")
print(dfp(cursor))
#排序
import pymongo
c = coll.find().sort('qty',pymongo.ASCENDING)
print("qty正序")
print(dfp(c))
c = coll.find().sort([("size.h",pymongo.DESCENDING),('qty',-1)])
print("先size、qty降序")
print(dfp(c))
# 更新
result = coll.update_many({"status":"D"},{"$inc":{"qty":5}})
cursor = coll.find()
print("更新D-qty+5")
print(dfp(cursor))
#删除
result = coll.delete_one({"item":"paper"})
cursor = coll.find()
print("删除-paper")
print(dfp(cursor))
#索引
coll.create_index([("qty",pymongo.DESCENDING)], name="qty")
ins = coll.index_information()
print("按照qtt创建索引降序")
print(ins)
# 删除索引
coll.drop_index('qty')
print('删除索引qty')
ins = coll.index_information()
print(ins)
# 聚合管道
# 与mongodb shell写法接近,stage用数组表示每个步骤有序,注意运算符都要加双引号“”
# Project:
c = coll.aggregate([{"$project":{"_id":0, "aa":"$status"}}])
print("Project")
print(dfp(c))
# Match
c = coll.aggregate([{"$match":{"status":"A"}}])
print("Match")
print(dfp(c))
# Group
c = coll.aggregate([{"$group":{"_id":"$status", "count":{"$sum":1}, "qs":{"$avg":"$qty"}}}])
print("Group")
print(dfp(c))
输出的结果如下
所有数据逐个输出
{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}
{'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper', 'qty': 100.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'}
{'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner', 'qty': 75.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'}
{'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}
所有数据
[{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}, {'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}, {'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}, {'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper', 'qty': 100.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'}, {'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner', 'qty': 75.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'}, {'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}]
条件查询
[{'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}]
[{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}, {'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}, {'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper', 'qty': 100.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'}, {'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner', 'qty': 75.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'}, {'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}]
[{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}, {'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}, {'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}, {'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper', 'qty': 100.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'}, {'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner', 'qty': 75.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'}, {'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}]
[{'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}]
投影
{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal'}
{'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal'}
{'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook'}
{'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper'}
{'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner'}
{'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard'}
------------------------------------------
分页
{'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper', 'qty': 100.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'}
------------------------------------------
qty正序
{'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}
{'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner', 'qty': 75.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'}
{'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper', 'qty': 100.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'}
------------------------------------------
先size、qty降序
{'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner', 'qty': 75.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'}
{'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}
{'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper', 'qty': 100.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'}
{'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}
------------------------------------------
更新D-qty+5
{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}
{'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb89673a'), 'item': 'paper', 'qty': 105.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'D'}
{'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner', 'qty': 80.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'}
{'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}
------------------------------------------
删除-paper
{'_id': ObjectId('63847783146f7f64cb896737'), 'item': 'journal', 'qty': 55.0, 'size': {'h': 11.0, 'w': 22.0, 'uom': 'in'}, 'status': 'C'}
{'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb89673b'), 'item': 'planner', 'qty': 80.0, 'size': {'h': 22.85, 'w': 30.0, 'uom': 'cm'}, 'status': 'D'}
{'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}
------------------------------------------
按照qtt创建索引降序
{'_id_': {'v': 2, 'key': [('_id', 1)], 'ns': 'primer.colletion8'}, 'qty': {'v': 2, 'key': [('qty', -1)], 'ns': 'primer.colletion8'}}
删除索引qty
{'_id_': {'v': 2, 'key': [('_id', 1)], 'ns': 'primer.colletion8'}}
Project
{'aa': 'C'}
{'aa': 'A'}
{'aa': 'A'}
{'aa': 'D'}
{'aa': 'A'}
------------------------------------------
Match
{'_id': ObjectId('63847783146f7f64cb896738'), 'item': 'journal', 'qty': 25.0, 'size': {'h': 14.0, 'w': 21.0, 'uom': 'cm'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb896739'), 'item': 'notebook', 'qty': 50.0, 'size': {'h': 8.5, 'w': 11.0, 'uom': 'in'}, 'status': 'A'}
{'_id': ObjectId('63847783146f7f64cb89673c'), 'item': 'postcard', 'qty': 45.0, 'size': {'h': 10.0, 'w': 15.25, 'uom': 'cm'}, 'status': 'A'}
------------------------------------------
Group
{'_id': 'D', 'count': 1, 'qs': 80.0}
{'_id': 'A', 'count': 3, 'qs': 40.0}
{'_id': 'C', 'count': 1, 'qs': 55.0}
------------------------------------------
进程已结束,退出代码 0