$

The $ positional operator identifies an element in an array to update without explicitly specifying the position of the element in the array.

$

The $ positional operator identifies an element in an array to update without explicitly specifying the position of the element in the array. The $ operator acts as a placeholder for the first element that matches the query condition, and the array field must appear as part of the query document.

Syntax

db.collection.updateOne(
  { <array>: <value> },
  { <update operator>: { "<array>.$": <value> } }
)

Parameters

ParameterDescription
arrayThe array field that contains the element to update. Must be part of the query condition.
valueThe value used to match the array element in the query condition.
update operatorThe update operator to apply (for example, $set, $inc, $unset).

Examples

Consider this sample document from the stores collection.

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

Example 1: Project the first element of an array, matching the condition

This query returns the first element within the salesByCategory array, for DJ equipment with totalSales greater than 35000.

db.stores.find({
    "sales.salesByCategory": {
        $elemMatch: {
            categoryName: {
                $regex: "^DJ"
            }
        }
    },
    "sales.salesByCategory.totalSales": {
        $gt: 35000
    }
}, {
    "sales.salesByCategory.$": 1
}).limit(2)

The first two results returned by this query are:

[
  {
    "_id": "d3c9df51-41bd-4b4e-a26b-b038d9cf8b45",
    "sales": {
      "salesByCategory": [
        {
          "categoryName": "DJ Speakers",
          "totalSales": 36972
        }
      ]
    }
  },
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "sales": {
      "salesByCategory": [
        {
          "categoryName": "DJ Headphones",
          "totalSales": 35911
        }
      ]
    }
  }
]

Example 2: Update discount percentage for a specific category

This query updates the discount percentage for "Desks" category in the first matching promotion event.

db.stores.updateOne(
  { 
    _id: "905d1939-e03a-413e-a9c4-221f74055aac",
    "promotionEvents.discounts.categoryName": "Desks"
  },
  {
    $set: { "promotionEvents.$.discounts.$[elem].discountPercentage": 25 }
  },
  {
    arrayFilters: [{ "elem.categoryName": "Desks" }]
  }
)

Example 3: Update sales category total

This query updates the total sales for a specific category using the $ (positional operator).

db.stores.updateOne(
  { 
    _id: "905d1939-e03a-413e-a9c4-221f74055aac",
    "sales.salesByCategory.categoryName": "Desk Lamps"
  },
  {
    $inc: { "sales.salesByCategory.$.totalSales": 1000 }
  }
)

Related content