$[] (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.

CategoryUpdate Modifier
MongoDB Version3.6+
Example Languagemongosh (Shell)

📝 Syntax

{ <update operator>: { '<array field>.$[]': <value> } }

⚙️ Parameters

array fieldThe array field to update wholly.
valueThe 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏