motor提供count_documents 方法查询文档数量,该方法也是一个异步方法,需要使用await关键字
import pprint
import asyncio
import motor.motor_asyncio
uri = "mongodb://kwsy:123456@localhost:27017/app"
client = motor.motor_asyncio.AsyncIOMotorClient(uri)
db = client.app
user = db.user
async def count_doc(collection, query):
n = await collection.count_documents({})
print(n)
n = await collection.count_documents(query)
print(n)
loop = asyncio.get_event_loop()
loop.run_until_complete(count_doc(user, {"age": 14}))
这一次封装的count_doc 函数接收两个参数,第一个参数是集合,第二个参数是查询条件
QQ交流群: 211426309