다음을 통해 공유


$pull

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

문법

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

매개 변수

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

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_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
                }
            ]
        }
    ]
}

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

태그 배열 필드에서 "#SeasonalSale" 값을 제거하려면 태그 필드에서 $pull 연산자를 사용하여 쿼리를 실행합니다.

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

이 쿼리는 다음 결과를 반환합니다.

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

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

endDate 연도가 2024이고 endDate 월이 3월 이전인 promotionEvents 배열에서 모든 요소를 제거하려면 지정된 날짜 값이 있는 promotionEvents 필드에서 $pull 연산자를 사용하여 쿼리를 실행합니다.

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
  }
]