- Reference >
mongo
Shell Methods >- Bulk Operation Methods >
- Bulk.find.upsert()
Bulk.find.upsert()¶
On this page
Tip
Starting in version 3.2, MongoDB also provides the
db.collection.bulkWrite()
method for performing bulk
write operations.
Description¶
-
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 totrue
, if no matching documents exist for theBulk.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:
Behavior¶
The following describe the insert behavior of various write operations
when used in conjunction with Bulk.find.upsert()
.
Insert for 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:
Insert for Bulk.find.updateOne()
¶
The Bulk.find.updateOne()
method accepts as its
parameter either:
- a replacement document that contains only field and value pairs (same
as
Bulk.find.replaceOne()
), - an update document that contains only update operator expressions, or
- an aggregation pipeline (Starting in MongoDB 4.2).
Field and Value Pairs¶
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:
Update Operator Expressions¶
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:
Aggregation Pipeline¶
Starting in version 4.2, update methods can accept an aggregation pipeline. For example, the following uses:
- the
$replaceRoot
stage which can provide somewhat similar behavior to a$setOnInsert
update operator expression, - the
$set
stage which can provide similar behavior to the$set
update operator expression, - the aggregation variable
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:
Insert for 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:
- an update document that contains only update operator expressions, or
- an aggregation pipeline (Starting in MongoDB 4.2).
Update Operator Expressions¶
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:
Aggregation Pipeline¶
Starting in version 4.2, update methods can accept an aggregation pipeline. For example, the following uses:
- the
$replaceRoot
stage which can provide somewhat similar behavior to a$setOnInsert
update operator expression, - the
$set
stage which can provide similar behavior to the$set
update operator expression, - the aggregation variable
NOW
, which resolves to the current datetime and can provide similar behavior to the$currentDate
update operator expression. The value ofNOW
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: