|
@@ -1,2 +1,36 @@
|
|
|
# motor
|
|
|
+MongoDB 的异步驱动
|
|
|
|
|
|
+## Usage
|
|
|
+
|
|
|
+```
|
|
|
+import asyncio
|
|
|
+from motor.motor_asyncio import AsyncIOMotorClient
|
|
|
+
|
|
|
+async def main():
|
|
|
+ # 连接 MongoDB 数据库
|
|
|
+ client = AsyncIOMotorClient('mongodb://localhost:27017')
|
|
|
+ db = client['test_database']
|
|
|
+ collection = db['test_collection']
|
|
|
+
|
|
|
+ # 插入文档
|
|
|
+ await collection.insert_one({'name': 'Alice', 'age': 30})
|
|
|
+
|
|
|
+ # 查询文档
|
|
|
+ cursor = collection.find({'age': {'$gte': 25}})
|
|
|
+ async for document in cursor:
|
|
|
+ print(document)
|
|
|
+
|
|
|
+ # 更新文档
|
|
|
+ await collection.update_one({'name': 'Alice'}, {'$set': {'age': 35}})
|
|
|
+
|
|
|
+ # 删除文档
|
|
|
+ await collection.delete_many({'age': {'$lt': 30}})
|
|
|
+
|
|
|
+ # 断开连接
|
|
|
+ client.close()
|
|
|
+
|
|
|
+# 运行主函数
|
|
|
+asyncio.run(main())
|
|
|
+
|
|
|
+```
|