$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行为

$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不保证修改后的集合中元素的特定顺序。

Missing Field缺失字段

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将创建指定值作为其元素的数组字段。

Field is Not an Array字段不是数组

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中的文档。

{ _id: 1, colors: "blue,green,red" }

The following $addToSet operation on the non-array field colors fails:以下对非数组字段colors$addToSet操作失败:

db.foo.update(
   { _id: 1 },
   { $addToSet: { colors: "c" } }
)

Value to Add is An Array要添加的值是一个数组

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中的文档:

{ _id: 1, letters: ["a", "b"] }

The following operation appends the array [ "c", "d" ] to the letters field:以下操作将数组[ "c", "d" ]追加到letters字段:

db.test.update(
   { _id: 1 },
   { $addToSet: { letters: [ "c", "d" ] } }
)

The letters array now includes the [ "c", "d" ] array as an element:letters数组现在包括[ "c", "d" ]数组作为一个元素:

{ _id: 1, letters: [ "a", "b", [ "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修饰符

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示例

Consider a collection inventory with the following document:考虑一个集合inventory带有以下文档:

{ _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.update(
   { _id: 1 },
   { $addToSet: { tags: "accessories" } }
)

Value Already Exists值已经存在

The following $addToSet operation has no effect as "camera" is already an element of the tags array:以下$addToSet操作无效,因为"camera"已经是标记数组的一个元素:

db.inventory.update(
   { _id: 1 },
   { $addToSet: { tags: "camera"  } }
)

$each Modifier修饰符

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包含以下文档:

{ _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.update(
   { _id: 2 },
   { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } }
 )

The operation adds only "camera" and "accessories" to the tags array since "electronics" already exists in the array:该操作仅向tags阵列添加"camera""accessories",因为阵列中已经存在"electronics"

{
  _id: 2,
  item: "cable",
  tags: [ "electronics", "supplies", "camera", "accessories" ]
}