- 插入数据
result = collection.insert_one(students)
result = collection.insert_many([students])
- 查询数据
result = collection.find_one({'name': 'Mike'})
# 大于20
results = collection.find({'age': {'$gt': 20}})
# 小于20
results = collection.find({'age': {'$lt': 20}})
results = collection.find({'age': 20})
for result in results:
print(result)
- 修改数据
condition = {'name': 'Mike'}
student = collection.find_one(condition)
student['age'] = 25
#
result = collection.update_one(condition, {'$set': student})
# 输出匹配数据条数、影响数据条数
print(result.matched_count, result.modified_count)
result = collection.update_many()
- 删除数据
result = collection.delete_one({'name':'kevin'})
result = collection.delete_many()
# 获得删除的数据条数
print(result.deleted_count)
5.排序
# ASCENDING升序,DESCENDING降序
results = collection.find().sort('age', pymongo.ASCENDING)
print([result['age'] for result in results])
标签:20,mongodb,age,results,collection,result,find
From: https://www.cnblogs.com/hellojacker/p/18068628