AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$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 を 更新演算子として使用する場合について説明します。

$addToSet は、これらの他のコンテキストでも使用できます。

次の環境でホストされる配置には $addToSet を使用できます。

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の 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.

Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $addToSet with an empty operand expression ( { } ). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).

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"] } )

次の操作で、letters フィールドに配列 [ "c", "d" ] が追加されます。

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

配列 [ "c", "d" ] は 1 つの要素として追加されます。

{ _id: 1, letters: [ 'a', 'b', [ 'c', 'd' ] ] }

Tip

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" が存在しないため、要素 "accessories" が tags 配列に追加されます。

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" ]
}
このページを評価