- Reference >
mongo
Shell Methods >- Bulk Operation Methods >
- Bulk.find.upsert()
Bulk.find.upsert()¶
On this page
Description¶
-
Bulk.find.
upsert
()¶ New in version 2.6.
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 the replacement document does not specify an _id
field, MongoDB adds the _id
field:
Insert for Bulk.find.updateOne()
¶
The Bulk.find.updateOne()
method accepts, as its parameter,
an <update>
document that contains only field and value pairs or
only update operator expressions.
Field and Value Pairs¶
If the <update>
document 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 <update>
document. If the update document does not specify an _id
field,
MongoDB adds the _id
field:
Update Operator Expressions¶
If the <update>
document contains 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 update from
the <update>
document:
If neither the <query>
document nor the <update>
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, 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 <query>
document nor the <update>
document
specifies an _id
field, MongoDB adds the _id
field.