$[]

Definition

  • $[]

New in version 3.6.

The all positional operator $[] indicates that the updateoperator should modify all elements in the specified array field.

The $[] operator has the following form:

  1. { <update operator>: { "<array>.$[]" : value } }

Use in update operations, e.g. db.collection.update() anddb.collection.findAndModify(), to modify all arrayelements for the document or documents that match the querycondition. For example:

  1. db.collection.updateMany(
  2. { <query conditions> },
  3. { <update operator>: { "<array>.$[]" : value } }
  4. )

For an example, see Update All Elements in an Array.

Behavior

upsert

If an upsert operation results in an insert, the query mustinclude an exact equality match on the arrayfield in order to use the $[] positional operator in the updatestatement.

For example, the following upsert operation, which uses $[] in theupdate document, specifies an exact equality match condition on thearray field:

  1. db.collection.update(
  2. { myArray: [ 5, 8 ] },
  3. { $set: { "myArray.$[]": 10 } },
  4. { upsert: true }
  5. )

If no such document exists, the operation would result in an insertionof the following document:

  1. { "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }

If the upsert operation did not include an exact equality match and nomatching documents were found to update, the upsert operation woulderror.

For example, the following operations would error if no matchingdocuments were found to update:

  1. db.collection.update(
  2. { myArray: 5 },
  3. { $set: { "myArray.$[]": 10 } },
  4. { upsert: true }
  5. )
  6.  
  7. db.collection.update(
  8. { },
  9. { $set: { "myArray.$[]": 10 } },
  10. { upsert: true }
  11. )

Nested Arrays

The $[] operator can be used for queries whichtraverse more than one array and nested arrays.

For an example, see Update Nested Arrays in Conjunction with $[<identifier>].

Examples

Update All Elements in an Array

Consider a collection students with the following documents:

  1. { "_id" : 1, "grades" : [ 85, 82, 80 ] }
  2. { "_id" : 2, "grades" : [ 88, 90, 92 ] }
  3. { "_id" : 3, "grades" : [ 85, 100, 90 ] }

To increment all elements in the grades array by 10 for alldocuments in the collection, use the all positional $[]operator:

  1. db.students.update(
  2. { },
  3. { $inc: { "grades.$[]": 10 } },
  4. { multi: true }
  5. )

The all positional $[] operator acts as aplaceholder for all elements in the array field.

After the operation, the students collection contains the followingdocuments:

  1. { "_id" : 1, "grades" : [ 95, 92, 90 ] }
  2. { "_id" : 2, "grades" : [ 98, 100, 102 ] }
  3. { "_id" : 3, "grades" : [ 95, 110, 100 ] }

Update All Documents in an Array

The $[] positional operator facilitates updates to arraysthat contain embedded documents. To access the fieldsin the embedded documents, use the dot notation on the $[] operator.

  1. db.collection.update(
  2. { <query selector> },
  3. { <update operator>: { "array.$[].field" : value } }
  4. )

Consider a collection students2 with the following documents:

  1. {
  2. "_id" : 1,
  3. "grades" : [
  4. { "grade" : 80, "mean" : 75, "std" : 8 },
  5. { "grade" : 85, "mean" : 90, "std" : 6 },
  6. { "grade" : 85, "mean" : 85, "std" : 8 }
  7. ]
  8. }
  9. {
  10. "_id" : 2,
  11. "grades" : [
  12. { "grade" : 90, "mean" : 75, "std" : 8 },
  13. { "grade" : 87, "mean" : 90, "std" : 5 },
  14. { "grade" : 85, "mean" : 85, "std" : 6 }
  15. ]
  16. }

To modify the value of the std field for all elements in thegrades array, use the positional $[] operator:

  1. db.students2.update(
  2. { },
  3. { $inc: { "grades.$[].std" : -2 } },
  4. { multi: true }
  5. )

After the operation, the collection has the following documents:

  1. {
  2. "_id" : 1,
  3. "grades" : [
  4. { "grade" : 80, "mean" : 75, "std" : 6 },
  5. { "grade" : 85, "mean" : 90, "std" : 4 },
  6. { "grade" : 85, "mean" : 85, "std" : 6 }
  7. ]
  8. }
  9. {
  10. "_id" : 2,
  11. "grades" : [
  12. { "grade" : 90, "mean" : 75, "std" : 6 },
  13. { "grade" : 87, "mean" : 90, "std" : 3 },
  14. { "grade" : 85, "mean" : 85, "std" : 4 }
  15. ]
  16. }

Update Arrays Specified Using a Negation Query Operator

Consider a collection results with the following documents:

  1. { "_id" : 1, "grades" : [ 85, 82, 80 ] }
  2. { "_id" : 2, "grades" : [ 88, 90, 92 ] }
  3. { "_id" : 3, "grades" : [ 85, 100, 90 ] }

To increment all elements in the grades array by 10 for alldocuments except those with the value 100 in the gradesarray, use the all positional $[] operator:

  1. db.results.update(
  2. { "grades" : { $ne: 100 } },
  3. { $inc: { "grades.$[]": 10 } },
  4. { multi: true }
  5. )

The all positional $[] operator acts as aplaceholder for all elements in the array field.

After the operation, the students collection contains the followingdocuments:

  1. { "_id" : 1, "grades" : [ 95, 92, 90 ] }
  2. { "_id" : 2, "grades" : [ 98, 100, 102 ] }
  3. { "_id" : 3, "grades" : [ 85, 100, 90 ] }

Update Nested Arrays in Conjunction with $[<identifier>]

The $[] positional operator, in conjunction with filter$[<identifier>] positional operator can be used to update nestedarrays.

Create a collection students3 with the following documents:

  1. db.students3.insert([
  2. { "_id" : 1,
  3. "grades" : [
  4. { type: "quiz", questions: [ 10, 8, 5 ] },
  5. { type: "quiz", questions: [ 8, 9, 6 ] },
  6. { type: "hw", questions: [ 5, 4, 3 ] },
  7. { type: "exam", questions: [ 25, 10, 23, 0 ] },
  8. ]
  9. }
  10. ])

To update all values that are greater than or equal to 8 in thenested grades.questions array, regardless of type:

  1. db.students3.update(
  2. {},
  3. { $inc: { "grades.$[].questions.$[score]": 2 } },
  4. { arrayFilters: [ { "score": { $gte: 8 } } ], multi: true}
  5. )

See also