- Reference >
- Operators >
- Update Operators >
- Array Update Operators >
- $[<identifier>]
$[<identifier>]¶
On this page
Definition¶
-
$[<identifier>]
¶ New in version 3.6.
The filtered positional operator
$[<identifier>]
identifies the array elements that match the arrayFilters conditions for an update operation, e.g.db.collection.update()
anddb.collection.findAndModify()
.Used in conjunction with the
arrayFilters
option, the$[<identifier>]
operator has the following form:Use in conjunction with the
arrayFilters
option to update all elements that match the arrayFilters conditions in the document or documents that match the query conditions. For example:Note
The
<identifier>
must begin with a lowercase letter and contain only alphanumeric characters.For an example, see Update All Array Elements That Match arrayFilters.
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 $[<identifier>]
in the update statement.
For example, the following upsert operation, which uses $[<identifier>]
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 insert of a document that resembles the following:
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:
The operation would return an error that resembles the following:
Nested Arrays¶
The filtered positional operator $[<identifier>]
can
be used for queries which traverse more than one array and nested arrays.
For an example, see Update Nested Arrays in Conjunction with $[].
Examples¶
Update All Array Elements That Match arrayFilters
¶
Consider a collection students
with the following documents:
To update all elements that are greater than or equal to 100
in the
grades
array, use the filtered positional operator
$[<identifier>]
with the arrayFilters:
The positional $[<identifier>]
operator acts as a
placeholder for all elements in the array field that match the
conditions specified in arrayFilters.
After the operation, the students
collection contains the following
documents:
Update All Documents That Match arrayFilters
in an Array¶
The $[<identifier>]
operator facilitates updates to arrays
that contain embedded documents. To access the fields
in the embedded documents, use the dot notation on the $[<identifier>]
.
Consider a collection students2
with the following documents:
To modify the value of the mean
field for all elements in the
grades
array where the grade is greater than or equal to 85
,
use the positional $[<identifier>]
operator and
arrayFilters:
After the operation, the collection has the following documents:
Update All Array Elements that Match Multiple Conditions¶
Consider a collection students2
with the following documents:
To modify the value of the std
field for all elements in the
grades
array where both the grade is greater than or equal to
80
and the std
is greater than or equal to 5
, use the
positional $[<identifier>]
operator and arrayFilters:
After the operation, the collection has the following documents:
Update Array Elements Using a Negation Operator¶
Consider a collection alumni
with the following documents:
To modify all elements in the degrees
array that do not have
"level": "Bachelor"
, use the positional [<identifier>]
operation with the $ne
query operator:
After the operation, the collection has the following documents:
Update Nested Arrays in Conjunction with $[]
¶
The $[<identifier>]
filtered positional operator, in conjunction
with the $[]
all positional operator, can be used to update
nested arrays.
Create a collection students3
with the following document:
The following updates the values that are greater than or equal to
8
in the nested grades.questions
array if the associated
grades.type
field is quiz
.
After the operation, the collection has the following document:
To update all values that are greater than or equal to 8
in the
nested grades.questions
array, regardless of type
: