このページには、Python アプリケーションをさまざまな設定で MongoDB に接続する方法を示すコード例が含まれています。
Tip
このページの接続オプションの詳細については、各セクションに記載されているリンクを参照してください。
このページの接続例を使用するには、コード例をサンプル アプリケーションまたは独自のアプリケーションにコピーします。 <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()
メソッドの詳細については、データ取得 ガイドの「 1 つのドキュメントの検索 」を参照してください。
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()
メソッドの詳細については、「個別のフィールド値の取得 」ガイドを参照してください。