- Reference >
- Operators >
- Update Operators >
- Array Update Operators >
- $[]
$[]¶
On this page
Definition¶
-
$[]
¶ New in version 3.6.
The all positional operator
$[]
indicates that the update operator should modify all elements in the specified array field.The
$[]
operator has the following form:Use in update operations, e.g.
db.collection.update()
anddb.collection.findAndModify()
, to modify all array elements for the document or documents that match the query condition. For example:For an example, see Update All Elements in an Array.
Behavior¶
upsert
¶
If an upsert operation results in an insert, the query
must
include an exact equality match on the array
field in order to use the $[]
positional operator in the update
statement.
For example, the following upsert operation, which uses $[]
in the
update document, specifies an exact equality match condition on the
array field:
If no such document exists, the operation would result in an insertion of the following document:
If the upsert operation did not include an exact equality match and no matching documents were found to update, the upsert operation would error.
For example, the following operations would error if no matching documents were found to update:
Nested Arrays¶
The $[]
operator can be used for queries which
traverse 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:
To increment all elements in the grades
array by 10
for all
documents in the collection, use the all positional $[]
operator:
The all positional $[]
operator acts as a
placeholder for all elements in the array field.
After the operation, the students
collection contains the following
documents:
Update All Documents in an Array¶
The $[]
positional operator facilitates updates to arrays
that contain embedded documents. To access the fields
in the embedded documents, use the dot notation on the $[]
operator.
Consider a collection students2
with the following documents:
To modify the value of the std
field for all elements in the
grades
array, use the positional $[]
operator:
After the operation, the collection has the following documents:
Update Arrays Specified Using a Negation Query Operator¶
Consider a collection results
with the following documents:
To increment all elements in the grades
array by 10
for all
documents except those with the value 100
in the grades
array, use the all positional $[]
operator:
The all positional $[]
operator acts as a
placeholder for all elements in the array field.
After the operation, the students
collection contains the following
documents:
Update Nested Arrays in Conjunction with $[<identifier>]
¶
The $[]
positional operator, in conjunction with filter
$[<identifier>]
positional operator can be used to update nested
arrays.
Create a collection students3
with the following documents:
To update all values that are greater than or equal to 8
in the
nested grades.questions
array, regardless of type
: