对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$addToSet(更新操作符)

$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 查找托管在以下环境中的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

The $addToSet operator has the form:

{ $addToSet: { <field1>: <value1>, ... } }

要在嵌入式文档或数组中指定 <field>,请使用点符号。

从 MongoDB 5.0 开始,更新操作符按字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。详情请参阅更新操作符行为

$addToSet only ensures that there are no duplicate items added to the set and does not affect existing duplicate elements. $addToSet does not guarantee a particular ordering of elements in the modified set.

从MongoDB5.0 开始,当您将 等更新操作符符与空操作数表达式()mongod $addToSet一起使用时,{ } 不再引发错误。空更新不会导致任何更改,也不会创建oplog条目(意味着该操作为“不操作”)。

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.

If you use $addToSet on a field that is not an array, the operation fails.

例如,创建 pigments 集合:

db.pigments.insertOne( { _id: 1, colors: "blue, green, red" } )

The colors field is not an array. The following $addToSet operation fails:

db.pigments.updateOne(
{ _id: 1 },
{ $addToSet: { colors: "mauve" } }
)

If the value is an array, $addToSet appends the whole array as a single element.

创建 alphabet 集合:

db.alphabet.insertOne( { _id: 1, letters: ["a", "b"] } )

以下操作会将数组 [ "c", "d" ] 追加到字段 letters 中:

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

添加数组 [ "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.

如果该值是一个文档,则当数组中的现有文档与待添加文档完全匹配时,MongoDB 会确定该文档是重复的。即,现有文档具有完全相同的字段和值,并且字段顺序相同。字段顺序很重要,您不能指定 MongoDB 仅比较文档中字段的子集以确定该文档是否为现有数组元素的重复项。

创建 inventory 集合:

db.inventory.insertOne(
{ _id: 1, item: "polarizing_filter", tags: [ "electronics", "camera" ] }
)

以下操作将元素 "accessories" 添加到 tags 数组中,因为数组中不存在 "accessories"

db.inventory.updateOne(
{ _id: 1 },
{ $addToSet: { tags: "accessories" } }
)

The following $addToSet operation has no effect because "camera" is already an element of the tags array:

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

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.

集合 inventory 包含以下文档:

db.inventory.insertOne (
{ _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:

db.inventory.updateOne(
{ _id: 2 },
{ $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } }
)

该操作仅将 "camera""accessories" 添加到 tags 数组。"electronics" 已在数组中:

{
_id: 2,
item: "cable",
tags: [ "electronics", "supplies", "camera", "accessories" ]
}
给本页内容打分