다음을 통해 공유


$pull(배열 업데이트)

$pull 연산자는 배열에서 조건과 일치하는 지정된 값 또는 값의 모든 인스턴스를 제거하는 데 사용됩니다. 이는 문서 내에서 배열 데이터를 정리하거나 수정해야 하는 경우에 유용합니다.

구문

{ $pull: { <field>: <value|condition> } }

매개 변수

설명
<field> 하나 이상의 값을 제거할 필드입니다.
<value|condition> 배열에서 제거할 값 또는 조건입니다.

예제

다음 샘플 json을 사용하여 사용량을 살펴보겠습니다.

{
  "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
   "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
  "location": {
    "lat": 60.1441,
    "lon": -141.5012
  },
  "staff": {
    "totalStaff": {
      "fullTime": 2,
      "partTime": 0
    }
  },
  "sales": {
    "salesByCategory": [
      {
        "categoryName": "DJ Headphones",
        "totalSales": 35921
      }
    ],
    "fullSales": 3700
  },
  "promotionEvents": [
    {
      "eventName": "Bargain Blitz Days",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 3,
          "Day": 11
        },
        "endDate": {
          "Year": 2024,
          "Month": 2,
          "Day": 18
        }
      },
      "discounts": [
        {
          "categoryName": "DJ Turntables",
          "discountPercentage": 18
        },
        {
          "categoryName": "DJ Mixers",
          "discountPercentage": 15
        }
      ]
    }
  ],
  "tag": [
    "#ShopLocal",
    "#SeasonalSale",
    "#FreeShipping",
    "#MembershipDeals"
  ]
}

예제 1: 배열에서 tag 특정 태그 제거

db.stores.update(
  { _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" },
  { $pull: { tag: "#SeasonalSale" } }
)

이 쿼리는 다음 문서를 반환합니다.

{
  "acknowledged": true,
  "insertedId": null,
  "matchedCount": "1",
  "modifiedCount": "1",
  "upsertedCount": 0
}

예제 2: 특정 날짜 이전에 끝나는 배열에서 promotionEvents 모든 이벤트 제거

db.stores.update(
  { _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" },
  { $pull: { promotionEvents: { "promotionalDates.endDate.Year": 2024, "promotionalDates.endDate.Month": { $lt: 3 } } } }
)

이 쿼리는 다음 문서를 반환합니다.

{
  "acknowledged": true,
  "insertedId": null,
  "matchedCount": "1",
  "modifiedCount": "1",
  "upsertedCount": 0
}

제한 사항

표준 Mongo 명령에서 제한/편차가 없으면 삭제하고 필요에 따라 업데이트합니다.