On this page本页内容
$addToSet
¶The $addToSet
operator adds a value to an array unless the value is already present, in which case $addToSet
does nothing to that array.$addToSet
运算符向数组中添加一个值,除非该值已经存在,在这种情况下,$addToSet
对该数组不做任何操作。
The $addToSet
operator has the form:$addToSet
运算符的形式如下:
To specify a 若要在嵌入式文档或数组中指定一个<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
$addToSet
only ensures that there are no duplicate items added to the set and does not affect existing duplicate elements. $addToSet
仅确保不会向集合中添加重复的项目,并且不会影响现有的重复元素。$addToSet
does not guarantee a particular ordering of elements in the modified set.$addToSet
不保证修改后的集合中元素的特定顺序。
If you use 如果在要更新的文档中没有的字段上使用$addToSet
on a field that is absent in the document to update, $addToSet
creates the array field with the specified value as its element.$addToSet
,$addToSet
将创建指定值作为其元素的数组字段。
If you use 如果在非数组的字段上使用$addToSet
on a field that is not an array, the operation will fail. $addToSet
,操作将失败。For example, consider a document in a collection 例如,考虑包含非数组字段foo
that contains a non-array field colors
.colors
的集合foo
中的文档。
The following 以下对非数组字段$addToSet
operation on the non-array field colors
fails:colors
的$addToSet
操作失败:
If the value is an array, 如果值是数组,$addToSet
appends the whole array as a single element.$addToSet
将整个数组作为单个元素追加。
Consider a document in a collection 考虑包含数组字段test
containing an array field letters
:letters
的集合test
中的文档:
The following operation appends the array 以下操作将数组[ "c", "d" ]
to the letters
field:[ "c", "d" ]
追加到letters
字段:
The letters
array now includes the [ "c", "d" ]
array as an element:letters
数组现在包括[ "c", "d" ]
数组作为一个元素:
Tip
To add each element of the value separately, use the 要单独添加值的每个元素,请将$each
modifier with $addToSet
. $each
修饰符与$addToSet
一起使用。See $each Modifier for details.有关详细信息,请参阅$each
修饰符。
If the value is a document, MongoDB determines that the document is a duplicate if an existing document in the array matches the to-be-added document exactly; i.e. the existing document has the exact same fields and values and the fields are in the same order. 如果该值是一个文档,如果数组中的现有文档与要添加的文档完全匹配,MongoDB将确定该文档是重复的;亦即,现有文档具有完全相同的字段和值,且字段顺序相同。As such, field order matters and you cannot specify that MongoDB compare only a subset of the fields in the document to determine whether the document is a duplicate of an existing array element.因此,字段顺序很重要,您不能指定MongoDB只比较文档中字段的子集,以确定文档是否与现有数组元素重复。
Consider a collection 考虑一个集合inventory
with the following document:inventory
带有以下文档:
The following operation adds the element 以下操作将元素"accessories"
to the tags
array since "accessories"
does not exist in the array:"accessories"
添加到tags
数组中,因为数组中不存在"accessories"
:
The following 以下$addToSet
operation has no effect as "camera"
is already an element of the tags
array:$addToSet
操作无效,因为"camera"
已经是标记数组的一个元素:
$each
You can use the 可以将$addToSet
operator with the $each
modifier. $addToSet
运算符与$each
修饰符一起使用。The $each
modifier allows the $addToSet
operator to add multiple values to the array field.$each
修饰符允许$addToSet
运算符向数组字段添加多个值。
A collection 集合inventory
has the following document:inventory
包含以下文档:
Then the following operation uses the 然后,以下操作使用$addToSet
operator with the $each
modifier to add multiple elements to the tags
array:$addToSet
运算符和$each
修饰符将多个元素添加到tags
数组中:
The operation adds only 该操作仅向"camera"
and "accessories"
to the tags
array since "electronics"
already exists in the array:tags
阵列添加"camera"
和"accessories"
,因为阵列中已经存在"electronics"
:
See also参阅
db.collection.update()
, db.collection.findAndModify()
, $push
, $pull