$addToSet
On this page本页内容
Definition定义
$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
运算符的形式如下:{ $addToSet: { <field1>: <value1>, ... } }
To specify a要在嵌入文档或数组中指定<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
Behavior行为
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
$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
不保证修改后的集合中元素的特定顺序。
Starting in MongoDB 5.0, 从MongoDB 5.0开始,当您将mongod
no longer raises an error when you use an update operator like $addToSet
with an empty operand expression ( { }
). $addToSet
之类的更新运算符与空操作数表达式({})一起使用时,mongod
不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。
Missing Field缺少字段
If you use 如果对要更新的文档中没有的字段使用$addToSet
on a field that is absent from the document to update, $addToSet
creates the array field with the specified value as its element.$addToSet
,$addToSet
将创建以指定值为元素的数组字段。
Field is Not an Array字段不是数组
If you use 如果在不是数组的字段上使用$addToSet
on a field that is not an array, the operation will fail.$addToSet
,则操作将失败。
For example, create the 例如,创建pigments
collection:pigments
集合:
db.pigments.insertOne( { _id: 1, colors: "blue, green, red" } )
The colors
field is not an array. colors
字段不是数组。The following 以下$addToSet
operation fails:$addToSet
操作失败:
db.pigments.updateOne(
{ _id: 1 },
{ $addToSet: { colors: "mauve" } }
)
Value to Add is An Array要添加的值是数组
If the value is an array, 如果值是一个数组,$addToSet
appends the whole array as a single element.$addToSet
会将整个数组作为单个元素追加。
Create the 创建alphabet
collection:alphabet
集合:
db.alphabet.insertOne( { _id: 1, letters: ["a", "b"] } )
The following operation appends the array 以下操作将数组[ "c", "d" ]
to the letters
field:[ "c", "d" ]
附加到letters
字段:
db.alphabet.updateOne(
{ _id: 1 },
{ $addToSet: { letters: [ "c", "d" ] } }
)
The array 数组[ "c", "d" ]
is added as a single element:[ "c", "d" ]
被添加为单个元素:
{ _id: 1, letters: [ 'a', 'b', [ 'c', 'd' ] ] }
To add each element of the value separately, use the 要分别添加值的每个元素,请使用$each
modifier with $addToSet
. See $each
Modifier for details.$each
修饰符和$addToSet
。有关详细信息,请参阅$each
修饰符。
Value to Add is a Document要添加的值是文档
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只比较文档中字段的子集来确定文档是否是现有数组元素的副本。
Examples实例
Create the 创建inventory
collection:inventory
集合:
db.inventory.insertOne(
{ _id: 1, item: "polarizing_filter", tags: [ "electronics", "camera" ] }
)
Add to Array添加到数组
The following operation adds the element 以下操作将元素"accessories"
to the tags
array since "accessories"
does not exist in the array:"accessories"
添加到tags
数组中,因为数组中不存在"accessories"
:
db.inventory.updateOne(
{ _id: 1 },
{ $addToSet: { tags: "accessories" } }
)
Value Already Exists值已存在
The following 以下$addToSet
operation has no effect because "camera"
is already an element of the tags
array:$addToSet
操作无效,因为"camera"
已经是tags
数组的一个元素:
db.inventory.updateOne(
{ _id: 1 },
{ $addToSet: { tags: "camera" } }
)
$each
Modifier修饰符
You can use the 您可以将$addToSet
operator with the $each
modifier. The $each
modifier allows the $addToSet
operator to add multiple values to the array field.$addToSet
运算符与$each
修饰符一起使用。$each
修饰符允许$addToSet
运算符向数组字段添加多个值。
A collection 集合inventory
has the following document:inventory
包含以下文档:
{ _id: 2, item: "cable", tags: [ "electronics", "supplies" ] }
Then the following operation uses the 然后,以下操作使用$addToSet
operator with the $each
modifier to add multiple elements to the tags
array:$addToSet
运算符和$each
修饰符将多个元素添加到tags
数组中:
db.inventory.updateOne(
{ _id: 2 },
{ $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } }
)
The operation only adds 该操作仅将"camera"
and "accessories"
to the tags
array. "electronics"
was already in the array:"camera"
和"accessories"
添加到tags
数组中。"electronics"
已经在数组中:
{
_id: 2,
item: "cable",
tags: [ "electronics", "supplies", "camera", "accessories" ]
}