Edit

Share via


$ (positional operator)

APPLIES TO: MongoDB vCore

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

The syntax for the $ positional operator is as follows:

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

Parameters

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

Example

Let's understand the usage with sample JSON from the stores dataset.

{
  "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
  "name": "Trey Research | Home Office Depot - Lake Freeda",
  "location": { "lat": -48.9752, "lon": -141.6816 },
  "staff": { "employeeCount": { "fullTime": 12, "partTime": 19 } },
  "sales": {
    "salesByCategory": [ { "categoryName": "Desk Lamps", "totalSales": 37978 } ],
    "revenue": 37978
  },
  "promotionEvents": [
    {
      "eventName": "Crazy Deal Days",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 9, "Day": 27 },
        "endDate": { "Year": 2023, "Month": 10, "Day": 4 }
      },
      "discounts": [
        { "categoryName": "Desks", "discountPercentage": 22 },
        { "categoryName": "Filing Cabinets", "discountPercentage": 23 }
      ]
    },
    {
      "eventName": "Incredible Markdown Mania",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 12, "Day": 26 },
        "endDate": { "Year": 2024, "Month": 1, "Day": 2 }
      },
      "discounts": [
        { "categoryName": "Monitor Stands", "discountPercentage": 20 },
        { "categoryName": "Desks", "discountPercentage": 24 }
      ]
    }
  ]
}

Example 1: Update discount percentage for a specific category

The example 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 2: Update sales category total

The example 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 }
  }
)

The query increases the total sales for "Desk Lamps" category by 1000, updating it from 37978 to 38978.

{
  "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
  "sales": {
    "salesByCategory": [ { "categoryName": "Desk Lamps", "totalSales": 38978 } ],
    "revenue": 37978
  }
}