本页包含的代码示例展示了如何使用各种设置将 Python 应用程序连接到 MongoDB。
提示
要了解有关此页面上的连接选项的更多信息,请参阅每个部分中提供的链接。
要使用此页面中的连接示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如 <hostname> )替换为 MongoDB 部署的相关值。
您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:
确保已安装 PyMongo。
复制以下代码并将其粘贴到新的.py文件中。
从此页面复制代码示例,并将其粘贴到文件中的指定行。
选择 Synchronous 或 Asynchronous标签页以查看相应的代码:
| 1 | import pymongo |
| 2 | from pymongo import MongoClient |
| 3 | |
| 4 | try: |
| 5 | uri = "<connection string URI>" |
| 6 | client = MongoClient(uri) |
| 7 | |
| 8 | database = client["<database name>"] |
| 9 | collection = database["<collection name>"] |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | client.close() |
| 16 | |
| 17 | except Exception as e: |
| 18 | raise Exception( |
| 19 | "The following error occurred: ", e) |
| 1 | import asyncio |
| 2 | import pymongo |
| 3 | from pymongo import AsyncMongoClient |
| 4 | |
| 5 | async def main(): |
| 6 | try: |
| 7 | uri = "<connection string URI>" |
| 8 | client = AsyncMongoClient(uri) |
| 9 | |
| 10 | database = client["<database name>"] |
| 11 | collection = database["<collection name>"] |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | await client.close() |
| 18 | |
| 19 | except Exception as e: |
| 20 | raise Exception( |
| 21 | "The following error occurred: ", e) |
| 22 | |
| 23 | asyncio.run(main()) |
| result = collection.insert_one({ "<field name>" : "<value>" }) |
|
| print(result.acknowledged) |
| result = await collection.insert_one({ "<field name>" : "<value>" }) |
|
| print(result.acknowledged) |
要学习;了解有关insert_one()方法的更多信息,请参阅插入文档指南。
| document_list = [ |
| { "<field name>" : "<value>" }, |
| { "<field name>" : "<value>" } |
| ] |
|
| result = collection.insert_many(document_list) |
|
| print(result.acknowledged) |
| document_list = [ |
| { "<field name>" : "<value>" }, |
| { "<field name>" : "<value>" } |
| ] |
|
| result = await collection.insert_many(document_list) |
|
| print(result.acknowledged) |
要了解有关insert_many()方法的更多信息,请参阅“插入文档”指南。
| query_filter = { "<field to match>" : "<value to match>" } |
| update_operation = { "$set" : |
| { "<field name>" : "<value>" } |
| } |
| result = collection.update_one(query_filter, update_operation) |
|
| print(result.modified_count) |
| query_filter = { "<field to match>" : "<value to match>" } |
| update_operation = { "$set" : |
| { "<field name>" : "<value>" } |
| } |
| result = await collection.update_one(query_filter, update_operation) |
|
| print(result.modified_count) |
要了解有关update_one()方法的更多信息,请参阅更新文档指南。
| query_filter = { "<field to match>" : "<value to match>" } |
| update_operation = { "$set" : |
| { "<field name>" : "<value>" } |
| } |
| result = collection.update_many(query_filter, update_operation) |
|
| print(result.modified_count) |
| query_filter = { "<field to match>" : "<value to match>" } |
| update_operation = { "$set" : |
| { "<field name>" : "<value>" } |
| } |
| result = await collection.update_many(query_filter, update_operation) |
|
| print(result.modified_count) |
要了解有关update_many()方法的更多信息,请参阅更新文档指南。
| query_filter = { "<field to match>" : "<value to match>" } |
| replace_document = { "<new document field name>" : "<new document value>" } |
|
| result = collection.replace_one(query_filter, replace_document) |
|
| print(result.modified_count) |
| query_filter = { "<field to match>" : "<value to match>" } |
| replace_document = { "<new document field name>" : "<new document value>" } |
|
| result = await collection.replace_one(query_filter, replace_document) |
|
| print(result.modified_count) |
要了解有关replace_one()方法的更多信息,请参阅替换文档指南。
| query_filter = { "<field to match>" : "<value to match>" } |
|
| result = collection.delete_one(query_filter) |
|
| print(result.deleted_count) |
| query_filter = { "<field to match>" : "<value to match>" } |
|
| result = await collection.delete_one(query_filter) |
|
| print(result.deleted_count) |
要了解有关delete_one()方法的更多信息,请参阅“删除文档”指南。
| query_filter = { "<field to match>" : "<value to match>" } |
|
| result = collection.delete_many(query_filter) |
|
| print(result.deleted_count) |
| query_filter = { "<field to match>" : "<value to match>" } |
|
| result = await collection.delete_many(query_filter) |
|
| print(result.deleted_count) |
要了解有关delete_many()方法的更多信息,请参阅“删除文档”指南。
| operations = [ |
| pymongo.InsertOne( |
| { |
| "<field name>" : "<value>" |
| } |
| ), |
| pymongo.UpdateMany( |
| { "<field to match>" : "<value to match>" }, |
| { "$set" : { "<field name>" : "<value>" }}, |
| ), |
| pymongo.DeleteOne( |
| { "<field to match>" : "<value to match>" } |
| ), |
| ] |
|
| result = collection.bulk_write(operations) |
|
| print(result) |
| operations = [ |
| pymongo.InsertOne( |
| { |
| "<field name>" : "<value>" |
| } |
| ), |
| pymongo.UpdateMany( |
| { "<field to match>" : "<value to match>" }, |
| { "$set" : { "<field name>" : "<value>" }}, |
| ), |
| pymongo.DeleteOne( |
| { "<field to match>" : "<value to match>" } |
| ), |
| ] |
|
| result = await collection.bulk_write(operations) |
|
| print(result) |
要了解有关bulk_write()方法的更多信息,请参阅批量写入指南。
| results = collection.find_one({ "<field name>" : "<value>" }) |
|
| print(results) |
| results = await collection.find_one({ "<field name>" : "<value>" }) |
|
| print(results) |
要了解有关find_one()方法的更多信息,请参阅“检索数据”指南中的“查找一个文档”。
| results = collection.find({ "<field name>" : "<value>" }) |
|
| for document in results: |
| print(document) |
| results = collection.find({ "<field name>" : "<value>" }) |
|
| async for document in results: |
| print(document) |
要了解有关find()方法的更多信息,请参阅检索数据指南中的查找多个文档。
| count = collection.count_documents({}) |
|
| print(count) |
| count = await collection.count_documents({}) |
|
| print(count) |
要了解有关count_documents()方法的更多信息,请参阅《检索准确计数》指南。
| count = collection.count_documents({ "<field name>": "<value>" }) |
|
| print(count) |
| count = await collection.count_documents({ "<field name>": "<value>" }) |
|
| print(count) |
要了解有关count_documents()方法的更多信息,请参阅《检索准确计数》指南。
| count = collection.estimated_document_count() |
|
| print(count) |
| count = await collection.estimated_document_count() |
|
| print(count) |
要了解有关estimated_document_count()方法的更多信息,请参阅“检索估计计数”指南。
| results = collection.distinct("<field name>") |
|
| for document in results: |
| print(document) |
| results = await collection.distinct("<field name>") |
|
| for document in results: |
| print(document) |
要了解有关distinct()方法的更多信息,请参阅“检索不同字段值”指南。