$pull

O $pull operador é usado para remover todas as instâncias de um valor especificado ou valores que correspondem a uma condição de uma matriz. Isso é útil quando você precisa limpar ou modificar dados de matriz em seus documentos.

Sintaxe

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

Parâmetros

Parâmetro Description
<field> O campo do qual remover um ou mais valores.
<value|condition> O valor ou condição a ser removido da matriz.

Examples

Considere este documento de exemplo da coleção de lojas.

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

Exemplo 1: Remover uma tag específica da tag matriz

Para remover o valor "#SeasonalSale" do campo de matriz de tags, execute uma consulta usando o operador $pull no campo de tag.

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

Esta consulta retorna o seguinte resultado.

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

Exemplo 2: Remover todos os eventos da promotionEvents matriz que terminam antes de uma determinada data

Para remover todos os elementos da matriz promotionEvents em que o endDate year é 2024 e o endDate month é anterior a março, execute uma consulta usando o operador $pull no campo promotionEvents com os valores de data especificados.

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

Esta consulta retorna o seguinte resultado.

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