你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
运算符 $pullAll 用于从数组中删除指定值的所有实例。 当需要通过在单个操作中删除多个特定元素来清理数组时,此运算符非常有用。
这两个元素 $pull 都 $pullAll 用于从数组中删除元素,但它们在标识要删除的元素的方式上有所不同。
$pull 从与特定条件匹配的数组中删除所有元素,该数组可以是简单值或更复杂的查询(如匹配子文档字段)。 另一方面, $pullAll 删除作为完全匹配项数组提供的特定值,但不支持条件或查询。 本质上,它更灵活, $pull 因为它允许根据各种条件进行条件删除,而 $pullAll 更简单,只处理一组固定的值。
Syntax
{
$pullAll: { <field1>: [ <value1>, <value2>] }
}
参数
| 参数 | Description |
|---|---|
<field1> |
将删除指定值的字段。 |
[ <value1>, <value2>, ... ] |
要从指定字段中删除的值数组。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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”数组中删除“#MembershipDeals”和“#SeasonalSale”的折扣,请使用标记字段上的$pulAll运算符和要删除的值运行查询。
db.stores.updateMany(
//filter
{ _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"},
{
$pullAll: {
tag: ["#MembershipDeals","#SeasonalSale" ]
}
}
)
此查询返回以下结果。
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": 1,
"modifiedCount": 1,
"upsertedCount": 0
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。