定義
$addToSetThe
$addToSetoperator adds a value to an array unless the value is already present, in which case$addToSetdoes nothing to that array.
注意
曖昧さ回避
このページでは、$addToSet を 更新演算子として使用する場合について説明します。
$addToSet は、これらの他のコンテキストでも使用できます。
$addToSet (accumulator), 可変数の入力ドキュメントに基づいて単一の集計値を返します。$addToSet (window function)を使用して、$setWindowFieldsステージの特定のウィンドウ内のドキュメントから一意の値の配列を返します。
互換性
次の環境でホストされる配置には $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" } } )
$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.
コレクション 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" ] }