Array update operators

Array update operators allow you to modify the elements of an array field in a document.

OperatorDescription
$pushAdds an element to an array
$addToSetAdds elements to a specific array as long as the element does not already exist in the array
$popRemoves either the first or the last element of an array
$pullAllRemoves all matching values in a specified query from an array

$push

The $push operator updates a document by adding an element to a specified array. If the array does not exist, a new array is created with the element added to it.

Insert the following document into a store collection:

  1. db.store.insertMany([
  2. { _id: 1, items: ['pens', 'pencils', 'paper', 'erasers', 'rulers'] }
  3. ])

Example: Use the $push operator to add an element to an existing array.

  1. db.store.updateOne({ _id: 1 }, { $push: { items: 'markers' } })

After the operation, the updated document looks like this:

  1. [
  2. {
  3. _id: 1,
  4. items: ['pens', 'pencils', 'paper', 'erasers', 'rulers', 'markers']
  5. }
  6. ]

$addToSet

The $addToSet operator updates an array by adding a specified element to an array if the element does not already exist in the array. If the specified element exists in the array, the $addToSet operator will not modify the array.

Insert the following documents into a store collection:

  1. db.store.insertMany([{ _id: 1, items: ['pens', 'pencils'] }])

Example: Use the $addToSet operator to update the array with non-existing elements.

  1. db.store.updateOne({ _id: 1 }, { $addToSet: { items: 'paper' } })

The document is subsequently updated with the new element, as depicted below:

  1. [{ _id: 1, items: ['pens', 'pencils', 'paper'] }]

Example: Use the $addToSet operator to update the array with already existing elements.

  1. db.store.updateOne({ _id: 1 }, { $addToSet: { items: 'pens' } })

Since the array already contains the element, there won’t be any changes.

  1. [{ _id: 1, items: ['pens', 'pencils', 'paper'] }]

Array update operators - 图1note

The $addToSet is different from the $push operator which adds the element to the array either it exists or not.

Example: Use the $addToSet operator for non-existing array fields.

If the array field does not exist in the document, the $addToSet operator will create the field and add the element to the array.

  1. db.store.updateOne({ _id: 1 }, { $addToSet: { colors: 'red' } })

The updated document looks like this:

  1. [{ _id: 1, items: ['pens', 'pencils', 'paper'], colors: ['red'] }]

$pop

With the $pop operator, you can update a document by removing the first or last element of an array. Assign a value of -1 to remove the first element of an array, or 1 to remove the last element.

Insert this document into a products collection:

  1. db.products.insertMany([
  2. { _id: 1, items: ['pens', 'pencils', 'paper', 'erasers', 'rulers'] }
  3. ])

Example: Use the $pop operator to remove the first element of an array.

  1. db.products.updateOne({ _id: 1 }, { $pop: { items: -1 } })

The document is subsequently updated with the first element pens removed, as depicted below:

  1. [
  2. {
  3. _id: 1,
  4. items: ['pencils', 'paper', 'erasers', 'rulers']
  5. }
  6. ]

To remove the last element of the array, assign 1 as the value for the $pop operator.

  1. db.products.updateOne({ _id: 1 }, { $pop: { items: 1 } })

The updated now looks like this:

  1. [
  2. {
  3. _id: 1,
  4. items: ['pencils', 'paper', 'erasers']
  5. }
  6. ]

$pullAll

The $pullAll operator removes all the matching elements in a specified query from an array.

Insert the following document into a store collection:

  1. db.store.insertMany([
  2. { _id: 1, items: ['pens', 'pencils', 'paper', 'erasers', 'rulers'] }
  3. ])

Example: Use the $pullAll operator to remove multiple elements from an array.

  1. db.store.updateOne(
  2. { _id: 1 },
  3. { $pullAll: { items: ['pens', 'pencils', 'paper'] } }
  4. )

After removing all instances of the specified array elements, the document is updated as follows:

  1. [
  2. {
  3. _id: 1,
  4. items: ['erasers', 'rulers']
  5. }
  6. ]

Example: Use the $pullAll operator to remove array of objects from an array.

Insert the following document into a fruits collection:

  1. db.fruits.insertMany([
  2. {
  3. _id: 1,
  4. fruits: [
  5. { type: 'apple', color: 'red' },
  6. { type: 'banana', color: 'yellow' },
  7. { type: 'orange', color: 'orange' }
  8. ]
  9. }
  10. ])

The following query uses the $pullAll to remove all matching array objects from the specified document.

  1. db.fruits.update(
  2. { _id: 1 },
  3. {
  4. $pullAll: {
  5. fruits: [
  6. { type: 'apple', color: 'red' },
  7. { type: 'banana', color: 'yellow' }
  8. ]
  9. }
  10. }
  11. )

The updated document now looks like this:

  1. [{ _id: 1, fruits: [{ type: 'orange', color: 'orange' }] }]