$[identifier] (Filtered Positional)
Last updated: 2026-10-09
$[identifier] (Filtered Positional)
$[identifier] is the positional operator that, together with arrayFilters, updates only the array elements matching a condition. `{ $set: { 'grades.$[elem]': 90 } }, { arrayFilters: [{ 'elem.grade': { $gte: 85 } }] }`. The most flexible array update, introduced in MongoDB 3.6; identifiers are defined in arrayFilters and can match nested document fields.
| Category | Update Modifier |
|---|---|
| MongoDB Version | 3.6+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <update operator>: { '<array field>.$[<identifier>]': <value> } }, { arrayFilters: [ { '<identifier>.<field>': <condition> } ] }
⚙️ Parameters
| array field | The array field to update. |
|---|---|
| identifier | A placeholder defined in arrayFilters, e.g. elem. |
| arrayFilters | The condition array defining identifier matching rules. |
Returns:The update result
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $[elem]: update only grades >= 85
// arrayFilters defines the elem rule
db.students.updateOne(
{ _id: 1 },
{ $set: { 'grades.$[elem]': 100 } },
{ arrayFilters: [{ 'elem': { $gte: 85 } }] }
)
Output:
// Only grades of 85 or higher become 100; the rest stay unchanged
📌 Notes
- Identifiers in arrayFilters carry no $ prefix
- Supports nested documents and multi-field conditions
- Cannot mix with $ or $[] on the same update path
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $[elem] and $[]?
A$[] updates all elements; $[elem] with arrayFilters updates only matching ones.
QHow do I write arrayFilters?
A{ arrayFilters: [{ 'elem': { $gte: 85 } }] }; identifiers have no $ prefix.
QCan I filter on nested fields?
AYes, e.g. { 'elem.type': 'exam' }.