On this page本页内容
Tip
Starting in version 3.2, MongoDB also provides the db.collection.bulkWrite()
method for performing bulk write operations.
Bulk.find.
upsert
()¶Sets the upsert option to true for an update or a replacement operation and has the following syntax:语法如下所示:
With the upsert
option set to true
, if no matching documents exist for the Bulk.find()
condition, then the update or the replacement operation performs an insert. If a matching document does exist, then the update or replacement operation performs the specified update or replacement.
Use Bulk.find.upsert()
with the following write operations:
The following describe the insert behavior of various write operations when used in conjunction with Bulk.find.upsert()
.
Bulk.find.replaceOne()
¶The Bulk.find.replaceOne()
method accepts, as its parameter, a replacement document that only contains field and value pairs:
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:
Bulk.find.updateOne()
¶The Bulk.find.updateOne()
method accepts as its parameter either:
Bulk.find.replaceOne()
),If the parameter is a replacement document that contains only field and value pairs:
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:
If the parameter is an update document that contains only update operator expressions:
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:
Starting in version 4.2, update methods can accept an aggregation pipeline. For example, the following uses:
$replaceRoot
stage which can provide somewhat similar behavior to a $setOnInsert
update operator expression,$set
stage which can provide similar behavior to the $set
update operator expression,NOW
, which resolves to the current datetime and can provide similar behavior to a $currentDate
update operator expression.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:
Bulk.find.update()
¶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.
The Bulk.find.update()
method accepts as its parameter either:
If the parameter is an update document that contains only update operator expressions:
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:
Starting in version 4.2, update methods can accept an aggregation pipeline. For example, the following uses:
$replaceRoot
stage which can provide somewhat similar behavior to a $setOnInsert
update operator expression,$set
stage which can provide similar behavior to the $set
update operator expression,NOW
, which resolves to the current datetime and can provide similar behavior to the $currentDate
update operator expression. The value of NOW
remains the same throughout the pipeline. To access aggregation variables, prefix the variable with double dollar signs $$
and enclose in quotes.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: