On this page本页内容
$setOnInsert
¶If an update operation with upsert: true results in an insert of a document, then 如果$setOnInsert
assigns the specified values to the fields in the document. upsert:true
的更新操作导致插入文档,则$setOnInsert
会将指定的值分配给文档中的字段。If the update operation does not result in an insert, 如果更新操作没有导致插入,$setOnInsert
does nothing.$setOnInsert
不执行任何操作。
You can specify the 您可以为upsert
option for either the db.collection.update()
or db.collection.findAndModify()
methods.db.collection.update()
方法和db.collection.findAndModify()
方法指定upsert
选项。
To specify a 若要在嵌入式文档或数组中指定一个<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
A collection named 名为products
contains no documents.products
的集合不包含文档。
Then, the following 然后,下面的db.collection.update()
with upsert: true inserts a new document.db.collection.update()
方法使用upsert:true
插入新文档。
MongoDB creates a new document with MongoDB根据_id
equal to 1
from the <query>
condition, and then applies the $set
and $setOnInsert
operations to this document.<query>
条件创建一个_id
等于1
的新文档,然后将$set
和$setOnInsert
操作应用于该文档。
The products
collection contains the newly-inserted document:products
集合包含新插入的文档:
If the 如果db.collection.update()
with upsert: true had found a matching document, then MongoDB performs an update, applying the $set
operation but ignoring the $setOnInsert
operation.db.collection.update()
使用upsert:true
已找到匹配的文档,然后MongoDB执行更新,应用$set
操作,但忽略$setOnInsert
操作。
See also参阅