pymongo简单使用

代码:

# -*- coding: utf-8 -*-
# @Time : 2020/4/25 15:09
# @Author : Oneqq
# @File : 1.mongo.py
# @Software: PyCharm

import pymongo

client = pymongo.MongoClient()
person = client.person
student = person.student
result = student.find()
for r in result:
    print(r)

print(result.next())
print(result.next())
print(result.next())

result = student.find({"name": "张无忌"})
result = student.find().sort("age", 1)
result = student.find().sort("age", pymongo.DESCENDING)
result = student.find().limit(3).skip(2)
for r in result:
    print(r)

# 查询数量
result = student.estimated_document_count()
# 有条件查询数量
result = student.count_documents({'name': "张无忌"})
print(result)

data = {"name": "黄月英", "age": "19"}
data = [{"name": "小乔"}, {"name": "大乔"}]
result = student.insert_many(data)

result = student.delete_one(data)

data = {"name": "小乔"}
result = student.find_one(data)
# print(result)
result['age'] = '18'
student.update_one(data, {"$set": result})

结果:

pymongo简单使用_第1张图片

你可能感兴趣的:(Python)