AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

Bulk.find.upsert()(mongoshメソッド)

Tip

MongoDB は、一括書き込み操作を実行するための db.collection.bulkWrite() メソッドも提供します。

Bulk.find.upsert()

更新操作または置換操作の upsert オプションを true に設定します。構文は以下のようになります。

Bulk.find(<query>).upsert().update(<update>);
Bulk.find(<query>).upsert().updateOne(<update>);
Bulk.find(<query>).upsert().replaceOne(<replacement>);

upsert オプションが true に設定されている場合、Bulk.find() 条件に一致するドキュメントが存在しない場合は、更新操作または置換操作によって挿入が実行されます。一致するドキュメントが存在する場合には、更新操作または置換操作は、指定された更新または置換を実行します。

Use Bulk.find.upsert() with the following write operations:

このコマンドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです

注意

このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。

The following describe the insert behavior of various write operations when used in conjunction with Bulk.find.upsert().

Bulk.find.replaceOne() メソッドは、フィールドと値のペアのみを含む置換ドキュメントをパラメータとして受け入れます。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { item: "abc123" } ).upsert().replaceOne(
{
item: "abc123",
status: "P",
points: 100,
}
);
bulk.execute();

If the replacement operation with the Bulk.find.upsert() option performs an insert, the inserted document is the replacement document. If neither the replacement document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("52ded3b398ca567f5c97ac9e"),
"item" : "abc123",
"status" : "P",
"points" : 100
}

Bulk.find.updateOne() メソッドは、次のいずれかをパラメータとして受け入れます。

  • フィールドと値のペアのみを含む置換ドキュメント(Bulk.find.replaceOne() と同じ)

  • 更新演算子式のみを含む更新ドキュメント

  • 集計パイプライン。

パラメータがフィールドと値のペアのみを含む置換ドキュメントの場合、次のようになります。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P" } ).upsert().updateOne(
{
item: "TBD",
points: 0,
inStock: true,
status: "I"
}
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the inserted document is the replacement document. If neither the replacement document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("52ded5a898ca567f5c97ac9f"),
"item" : "TBD",
"points" : 0,
"inStock" : true,
"status" : "I"
}

パラメータ更新演算子式のみを含む更新ドキュメントの場合、次のようになります。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P", item: null } ).upsert().updateOne(
{
$setOnInsert: { qty: 0, inStock: true },
$set: { points: 0 }
}
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a document with field and values from the query document of the Bulk.find() method and then applies the specified updates from the update document. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("5e28d1a1500153bc2872dadd"),
"item" : null,
"status" : "P",
"inStock" : true,
"points" : 0,
"qty" : 0
}

更新メソッドは集計パイプラインを受け入れることができます。 たとえば、次の例では次のものが使用されています。

  • $replaceRoot ステージ、これは $setOnInsert 更新演算子式と似た動作を提供できます。

  • $set ステージは、$set 更新演算子式と同様に動作します。

  • 集計変数 NOW、これは現在の日時を解決し、$currentDate 更新演算子式と同様に動作します。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { item: "Not Found", status: "P" } ).upsert().updateOne(
[
{ $replaceRoot: { newRoot: { $mergeObjects: [ { qty: 0, inStock: true }, "$$ROOT" ] } } },
{ $set: { points: 0, lastModified: "$$NOW" } }
]
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a document with field and values from the query document of the Bulk.find() method and then applies the specified aggregation pipeline. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("5e28cf1e500153bc2872d49f"),
"qty" : 0,
"inStock" : true,
"item" : "Not Found",
"status" : "P",
"points" : 0,
"lastModified" : ISODate("2020-01-22T22:39:26.789Z")
}

When using upsert() with the multiple document update method Bulk.find.update(), if no documents match the query condition, the update operation inserts a single document.

Bulk.find.update() メソッドは、次のいずれかをパラメータとして受け入れます。

  • 更新演算子式のみを含む更新ドキュメント

  • 集計パイプライン。

パラメータ更新演算子式のみを含む更新ドキュメントの場合、次のようになります。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P" } ).upsert().update(
{
$setOnInsert: { qty: 0, inStock: true },
$set: { status: "I", points: "0" }
}
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a single document with the fields and values from the query document of the Bulk.find() method and then applies the specified update from the update document. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id": ObjectId("52ded81a98ca567f5c97aca1"),
"status": "I",
"qty": 0,
"inStock": true,
"points": "0"
}

更新メソッドは集計パイプラインを受け入れることができます。 たとえば、次の例では次のものが使用されています。

  • $replaceRoot ステージ、これは $setOnInsert 更新演算子式と似た動作を提供できます。

  • $set ステージは、$set 更新演算子式と同様に動作します。

  • 集計変数 NOW、これは現在の日時に設定し、$currentDate 更新演算子式と同様の動作を提供できます。NOW の値はパイプライン全体で同じです。集計変数にアクセスするには、変数の前に二重ドル記号 $$ を付け、引用符で囲みます。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { item: "New Item", status: "P" } ).upsert().update(
[
{ $replaceRoot: { newRoot: { $mergeObjects: [ { qty: 0, inStock: true }, "$$ROOT" ] } } },
{ $set: { points: 0, lastModified: "$$NOW" } }
]
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a single document with the fields and values from the query document of the Bulk.find() method and then applies the aggregation pipeline. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("5e2920a5b4c550aad59d18a1"),
"qty" : 0,
"inStock" : true,
"item" : "New Item",
"status" : "P",
"points" : 0,
"lastModified" : ISODate("2020-01-23T04:27:17.780Z")
}
このページを評価