Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 893 Bytes

example-addToSet-each.rst

File metadata and controls

27 lines (19 loc) · 893 Bytes

A collection inventory has the following document:

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

Then the following operation uses the :update:`$addToSet` operator with the :update:`$each` modifier to add multiple elements to the tags array:

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:

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