Docs Menu
Docs Home
/ / /
PyMongo
/

ドキュメントの置換

項目一覧

  • Overview
  • サンプル データ
  • 置換操作
  • 必要なパラメーター
  • replaceOne
  • 置換操作をカスタマイズする
  • 戻り値
  • 詳細情報
  • API ドキュメント

このガイドでは、PyMongo を使用して MongoDB コレクション内のドキュメントに対して置換操作を実行する方法を学習します。 置換操作は更新操作とは異なります。 アップデート操作により、ターゲット ドキュメント内の指定されたフィールドのみが変更されます。 置換操作により、ターゲット ドキュメント内のすべてのフィールドが削除され、新しいフィールドに置き換えられます。

更新操作について詳しくは、 ドキュメントの更新 のガイドを参照してください。

このガイドの例では、 Atlas サンプル データセット sample_restaurants.restaurantsコレクションを使用します。 MongoDB Atlas クラスターを無料で作成して、サンプル データセットをロードする方法については、 「 PyMongo を使い始める 」チュートリアルを参照してください。

MongoDB では、 replace_one()メソッドを使用して置換操作を実行できます。 このメソッドは、検索条件に一致する最初のドキュメントから_idフィールドを除くすべてのフィールドを削除します。 その後、指定したフィールドと値がドキュメントに挿入されます。

replace_one() メソッドには次のパラメーターが必要です。

  • 置き換えるドキュメントを決定するクエリフィルタードキュメント 。 クエリフィルターの詳細については、 マニュアルの「 クエリフィルター ドキュメントMongoDB Server 」セクション を参照してください。

  • 置換ドキュメント。新しいドキュメントに挿入するフィールドと値を指定します。

次の例では、 replace_one()メソッドを使用して、ドキュメントのフィールドと値をnameフィールド値"Pizza Town"に置き換えます。

restaurants = database["restaurants"]
query_filter = {"name" : "Pizza Town"}
replace_document = { "name" : "Mongo's Pizza",
"cuisine" : "Pizza",
"address" : {
"street" : "123 Pizza St",
"zipCode" : "10003"
},
"borough" : "Manhattan"
}
result = restaurants.replace_one(query_filter, replace_document)

重要

_idフィールドの値は不変です。 置き換えドキュメントで_idフィールドに値が指定される場合は、既存のドキュメントの_id値と一致する必要があります。

オプションで、 replace_one()メソッドは追加のパラメーターを受け入れます。これらのパラメーターは、置換操作を構成するために使用できるオプションを表します。 追加のオプションを指定しない場合、ドライバーは置換操作をカスタマイズしません。

プロパティ
説明
upsert
Specifies whether the replace operation performs an upsert operation if no documents match the query filter. For more information, see the upsert statement in the MongoDB Server manual.
Defaults to False
bypass_document_validation
Specifies whether the replace operation bypasses document validation. This lets you replace documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see Schema Validation in the MongoDB Server manual.
Defaults to False.
collation
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
hint
Gets or sets the index to scan for documents. For more information, see the hint statement in the MongoDB Server manual.
session
An instance of ClientSession.
let
A Map of parameter names and values. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual.
comment
A comment to attach to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.

次のコードでは、 replace_one()メソッドを使用して、 nameフィールドの値が"Food Town"である最初のドキュメントを検索し、このドキュメントを"Food World"という名前の新しいドキュメントに置き換えます。 upsertオプションがTrueに設定されているため、クエリフィルターが既存のドキュメントと一致しない場合、ドライバーは新しいドキュメントを挿入します。

restaurants = database["restaurants"]
query_filter = {"name" : "Food Town"}
replace_document = { "name" : "Food World",
"cuisine" : "Mixed",
"address" : {
"street" : "123 Food St",
"zipCode" : "10003"
},
"borough" : "Manhattan"
}
result = restaurants.replace_one(query_filter, replace_document, upsert = True)

replace_one()メソッドはUpdateResultオブジェクトを返します。 UpdateResult型には次のプロパティが含まれています。

プロパティ
説明
matched_count
The number of documents that matched the query filter, regardless of how many were updated.
modified_count
The number of documents modified by the update operation. If an updated document is identical to the original, it is not included in this count.
raw_result
The raw result document returned by the server.
upserted_id
The ID of the document that was upserted in the database, if the driver performed an upsert. Otherwise None.

クエリフィルターの作成の詳細については、「クエリの指定」ガイドを参照してください。

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。

戻る

Update Documents