A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mongodb/mongodb-positional-operator/ below:

MongoDB - Positional Operator ($)

MongoDB - Positional Operator ($)

Last Updated : 12 Jul, 2025

MongoDB provides a variety of array update operators to modify the values within array fields in documents. One such operator is the positional operator $. This operator allows us to update an element in an array without explicitly specifying the position of that element. In this article, We will learn about the Positional Operator($) in MongoDB in detail.

MongoDB Positional Operator

MongoDB provides different types of array update operators to update the values of the array fields in the documents and the positional operator() is one of them.

The $ operator can be used with arrays containing embedded documents which allows us to update fields within those nested documents efficiently.

Syntax:

{ "<array>.$" : value }
Examples of MongoDB Positional Operator

In the following examples, we are working with called contributor which contains various information in terms of documents as shown below:

Output:

Example 1: Update All Elements in an Array

In this example, we are updating the first item whose value is "Python" to "Ruby" in the language field with the help of $ operator, because we don't know the position of the item in the array.

db.contributor.updateOne({name: "Rohit", language: "Python"}, 
{$set: {"language.$": "Ruby"}})

Output:

Here, the $ operator is working as a placeholder for the first match of the update query document.

Note: The array field must be the part of the query.

Example 2: Update All Documents in an Array

In this example, we are updating an array that contains embedded documents with the help of $ operator and to access embedded document fields we use dot notation. Or in other words, we are updating the value of tArticle field from 60 to 100.

db.contributor.updateOne({name: "Rohit", "articles.language": "C#"}, 
{$set: {"articles.$.tArticles": 100}})

Output:

Example 3: Update Nested Arrays 

In this example, we are updating the value of the tArticles field in the first embedded document that has a pArticles field with a value greater than 90.

db.contributor.updateOne({name: "Sumit", articles: {$elemMatch:
{pArticles: {$gt: 30}}}},
{$set: {"articles.$.tArticles": 200}})

Output:

Conclusion

The MongoDB positional operator $ is a powerful tool for updating elements within arrays when the exact position is unknown. By acting as a placeholder for the first matching element, it simplifies the update process. However, it comes with limitations such as not being usable with upsert operations or queries involving negation operators. Understanding these nuances can help effectively use the $ operator for various array update operations in MongoDB.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4