$[] (Update All Array Elements)
Last updated: 2026-10-09
$[] (Update All Array Elements)
$[] is the positional operator that updates every element of an array. `{ $set: { 'grades.$[]': 90 } }` sets every grade to 90. Combined with arrayFilters it updates only matching elements; without them it updates all. $[] belongs in update statements only — queries use $elemMatch instead.
| Category | Update Modifier |
|---|---|
| MongoDB Version | 3.6+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <update operator>: { '<array field>.$[]': <value> } }
⚙️ Parameters
| array field | The array field to update wholly. |
|---|---|
| value | The value applied to every element. |
Returns:The update result
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $[]: set every grade to 90
db.students.updateOne(
{ _id: 1 },
{ $set: { 'grades.$[]': 90 } }
)
// Nested arrays use $[] too
// Mark each level
db.students.updateOne(
{ _id: 1 },
{ $set: { 'scores.$[].math': 100 } }
)
Output:
// Every grade becomes 90; the second example sets each score's math to 100
📌 Notes
- Updates every element when no arrayFilters are given
- Conditional updates use $[identifier] with arrayFilters, not $[]
- Write $[] per level for nested arrays
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $[] and $?
A$ updates the first matched element; $[] updates every element.
QCan $[] work with conditions?
AConditions need $[identifier] with arrayFilters; $[] updates all unconditionally.
QCan $[] be used in queries?
ANo, it is for update statements only.