The Wayback Machine - https://web.archive.org/web/20260305042919/https://www.geeksforgeeks.org/mongodb/mongodb-all-positional-operator/

MongoDB All Positional Operator ($[])

Last Updated : 14 Feb, 2026

The all positional $[] operator in MongoDB updates every element in an array field for documents that match the query, enabling efficient in-place updates across arrays, including arrays of embedded documents.

Syntax

{ <update operator>: { "<array>.$[]" : <value> } }
  • Updates all elements within the specified array field.
  • Performs an in-place update on matched documents.

Examples of MongoDB All Positional Operator

In the following examples, we are working with: 

Database: GeeksforGeeks 
Collection: contributor 
Document: Two documents that contain the details of the contributor in the form of field-value pairs.

Screenshot-2026-02-14-105450

Example 1: Updating all the items in an array using All Positional Operator

Updating by incrementing all the items by 10 of points field. 

db.contributor.updateMany(
  {},
  { $inc: { "points.$[]": 5 } }
)

Output:

Screenshot-2026-02-14-105705

Example 2: Updating all the documents in the array using All Positional Operator

Updating by decrementing the value of the tArticles field by -10 for all the items in the articles array. 

db.contributor.updateMany(
  {},
  { $inc: { "articles.$[].tArticles": -10 } }
)

Output:

Screenshot-2026-02-14-105837

Example 3: Updating an array using a negation query operator

Incrementing all the items in the points array by 20 for all documents except those  with the value 100 in the points array. 

db.contributor.updateMany(
  { points: { $nin: [100] } },
  { $inc: { "points.$[]": 20 } }
)

Output:

Screenshot-2026-02-14-110108

Example 4: Updating the nested array in conjunction with $[< identifier>]

Updating all the values that are less than or equal to 80 in the nested marks.firstsemester array. 

db.contributor.updateMany(
  {},
  { $inc: { "marks.$[].firstsemester.$[newmarks]": 3 } },
  { arrayFilters: [{ newmarks: { $lte: 80 } }] }
)

Output:

Screenshot-2026-02-14-110235

Usage of All Positional Operator ($[])

The $[] positional operator updates all elements in a specified array field for documents that match the query, including nested arrays.

  • Update all elements within a specified array field in one operation.
  • Apply bulk updates to arrays containing embedded/nested documents.
  • Works with multi-level (nested) arrays in update queries.
  • With upsert enabled, requires an exact equality match on the array field; otherwise the upsert fails.
  • Use with update(), findAndModify() (legacy) and prefer updateOne() / updateMany() for modern updates.
Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment

Explore