Open In App

MongoDB All Positional Operator ($[])

Last Updated : 28 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The MongoDB $[] positional operator is a powerful tool used to update all elements in an array that match a specified condition. When working with arrays in MongoDB, this operator helps identify and modify every element that meets the query criteria.

In this article, We will learn about the MongoDB All Positional Operator ($[]) by understanding various examples.

MongoDB All Positional Operator

  • The MongoDB All Positional Operator $ is used to update elements in an array that match a specified condition.
  • When we query an array in MongoDB and want to update a specific element that matches a condition, the $ positional operator helps to identify the position of the matched element within the array.
  • All Positional Operator in MongoDB is used to modify all elements within a specified array field. It is very useful for updating arrays that contain embedded documents.
  • $[] operator is used 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> } }

Examples of MongoDB All Positional Operator

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.

Output:

demo database and collection

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

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

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

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

Example 3: Updating an array using a negation query operator

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

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

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.

Conclusion

The MongoDB $[] positional operator is a versatile and powerful feature for updating all elements within an array based on specific conditions. It streamlines the process of modifying arrays and nested arrays, making it an essential tool for developers working with complex MongoDB documents. By understanding and utilizing this operator, you can efficiently manage and update array data within your MongoDB collections.

FAQs on MongoDB All Positional Operator

What is the purpose of the $[] positional operator in MongoDB?

The $[] positional operator is used to update all elements in an array that match a specified condition. It allows for comprehensive updates across arrays with embedded documents.

Can the $[] operator be used with nested arrays?

Yes, the $[] operator can be used in conjunction with other operators to update nested arrays and traverse multiple arrays within a document.

What happens if the upsert option is used without an exact equality match on the array field?

If the `upsert` operation does not include an exact equality match on the array field, it will fail and produce an error.

Which MongoDB methods can utilize the $[] positional operator?

The $[] positional operator can be used with methods such as update(), updateOne(), updateMany(), and findAndModify() to modify all array items for documents that match the specified query condition.



Next Article

Similar Reads

three90RightbarBannerImg