Open In App

MongoDB All Positional Operator ($[])

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB All Positional Operator allows bulk updates to all elements in an array field that matches the given query condition.

All Positional Operators in MongoDB

The All Positional Operator in MongoDB, denoted by $[], is used to modify all elements within a specified array field. It is very useful for updating arrays that contain embedded documents.

The $[] operator is employed in update operations like db.collection.updateOne() and db.collection.findAndModify() to update all array elements for documents that meet the specified query condition.

Syntax

The syntax of All positional operator in MongoDB is:

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

MongoDB All Positional Operator Example

Let’s look at some examples of the All Positional Operator in MongoDB to understand it better.

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.

demo database and collection

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

In this example, we are updating by incrementing all the items by 10 of points field. 

Query:

db.contributor.update({}, {$inc: {&quot;points.$[]&quot;: 5}}, {multi: true})

Output:

updating all the items in an array using all positional operator

Updating all the documents in the array using All Positional Operator example

In this example, we are updating by decrementing the value of the tArticles field by -10 for all the items in the articles array. 

Query:

db.contributor.update({}, {$inc: {&quot;articles.$[].tArticles&quot;: -10}}, {multi: true})

Output:

updating all the documents in the array using all positional operator example output

Updating an array using a negation query operator example:

In this example, we are incrementing all the items in the points array by 20 for all documents except those  with the value 100 in the points array. 

Query:

db.contributor.update({points: {$ne: 25}}, {$inc: {&quot;points.$[]&quot;: 20}}, {multi: true})

Output:

updating an array using a negation query operator example output

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

In this example, we are updating all the values that are less than or equal to 80 in the nested marks.firstsemester array. 

Query:

db.contributor.update({}, {$inc: {&quot;marks.$[].firstsemester.$[newmarks]&quot;: 3}}, 
{arrayFilters: [{newmarks: {$lte: 80}}], multi: true})

Output:

updating the nested array in conjunction with $[< identifier>] example output

KeyTakeAways About MongoDB All Postional Operator ($[])

  • The $[] positional operator in MongoDB is used to modify all elements within a specified array field.
  • It facilitates updates to arrays that contain embedded documents, allowing for comprehensive updates across arrays with embedded documents.
  • You can also use this operator for those queries which traverse more than one array and nested arrays.
  • If upsert is set to true, then the query must contain an exact equality match on the array field in order to use $[] operator in the update statement. If upsert operation doesnot include the exact equality match on the array field, then upsert will give an error.
  • You can use $[] operator with update(), findAndModify(), etc., methods to modify all the array items for the document or documents that match the specified query condition.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads