Docs 菜单

Docs 主页开发应用程序MongoDB Manual

$pull

在此页面上

  • 兼容性
  • 语法
  • 行为
  • 举例
$pull

$pull操作符从现有数组中删除与指定条件匹配的一个或多个值的所有实例。

可以使用 $pull 查找托管在以下环境中的部署:

  • MongoDB Atlas :用于在云中部署 MongoDB 的完全托管服务

$pull操作符具有以下形式:

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

要在嵌入式文档或数组中指定 <field>,请使用点符号。

从 MongoDB 5.0 开始,更新操作符按字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。详情请参阅更新操作符行为

如果指定<condition>并且数组元素是嵌入式文档,则$pull操作符会应用<condition> ,就好像每个数组元素都是集合中的文档一样。有关示例,请参阅使用bulkWrite()删除与指定的$pull条件匹配的所有项。

如果要删除的指定<value>是数组,则$pull仅删除数组中与指定<value>完全匹配的元素,包括顺序。

如果要删除的指定<value>是文档,则$pull仅删除数组中具有完全相同字段和值的元素。字段的顺序可以不同。

从 MongoDB5 0mongod$pull{ }开始。当您将 等更新操作符与空操作数表达式 ( ) 一起使用时, 、 不再引发错误。空更新不会导致任何更改,也不会创建 oplog 条目(意味着该操作为“不操作”)。

创建 stores 集合:

db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )

以下操作会删除

  • "apples"fruits 数组中的 "oranges"

  • "carrots" vegetables 数组中的

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)

使用 db.collection.find() 确认结果:

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}

创建 profiles 集合:

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

以下操作将删除 votes 数组中大于或等于 ($gte) 6 的所有项:

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

更新操作后,文档中只有小于 6 的值:

{ _id: 1, votes: [ 3, 5 ] }

以下 db.collection.bulkWrite() 操作:

  • 创建 profilesBulkWrite 集合。

  • 删除 votes 数组中大于或等于 ($gte) 6 的所有项。

  • 删除 votes 数组中小于或等于 ($lte) 3 的所有项。

try {
db.profilesBulkWrite.bulkWrite( [
{
insertOne: {
"document": { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }
}
},
{
updateOne: {
"filter": { _id: 1 },
"update": { $pull: { votes: { $gte: 6 } } }
}
},
{
updateOne: {
"filter": {_id: 1},
"update": { $pull: { votes: { $lte: 3 } } }
}
}
] );
} catch (e) {
print(e);
}

注意

bulkWrite()

db.collection.bulkWrite() 方法用于执行数组中列出的多个写入操作。在本例中,db.collection.bulkWrite()profiles 集合执行了多个操作。

执行 db.collection.bulkWrite() 操作后,您可以使用以下操作确认文档中只有小于 6 且大于 3 的值:

db.profilesBulkWrite.find()

该操作返回以下内容:

[ { _id: 1, votes: [ 5 ] } ]

创建 survey 集合:

db.survey.insertMany([
{
_id: 1,
results: [
{ item: "A", score: 5 },
{ item: "B", score: 8 }
]
},
{
_id: 2,
results: [
{ item: "C", score: 8 },
{ item: "B", score: 4 }
]
}
] )

以下操作会删除 results 数组中同时包含等于 8score 字段和等于 "B"item 字段的所有元素:

db.survey.updateMany(
{ },
{ $pull: { results: { score: 8 , item: "B" } } }
)

$pull表达式将条件应用于results数组的每个元素,就像它是顶级文档一样。

执行此操作后,results 数组中不再有同时包含等于 8score 字段和等于 "B"item 字段的文档。

{ _id: 1, results: [ { item: 'A', score: 5 } ] },
{
_id: 2,
results: [ { item: 'C', score: 8 }, { item: 'B', score: 4 } ]
}

$pull操作符将每个元素视为顶级对象。查询会应用于每个元素。该表达式不需要使用$elemMatch来指定匹配条件。

相反,以下操作不会$pull原始集合中的任何元素:

db.survey.updateMany(
{ },
{ $pull: { results: { $elemMatch: { score: 8 , item: "B" } } } }
)

注意

删除 survey 集合,使用:

然后重新创建它以运行此示例。

使用嵌入在嵌套数组中的文档创建新的 survey 集合。

db.survey.drop()
db.survey.insertMany( [
{
_id: 1,
results: [
{
item: "A",
score: 5,
answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ]
},
{
item: "B",
score: 8,
answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ]
}
]
},
{
_id: 2,
results: [
{
item: "C",
score: 8,
answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ]
},
{
item: "B",
score: 4,
answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ]
}
]
}
] )

然后,您可以使用 $elemMatchanswers 数组中的元素指定多个条件:

db.survey.updateMany(
{ },
{
$pull:
{
results:
{
answers: { $elemMatch: { q: 2, a: { $gte: 8 } } }
}
}
}
)

该操作更新了每个匹配文档中的 results 数组。当嵌入式 answers 数组的元素与突出显示行中的选择条件相匹配时,db.collection.updateMany()results 中删除了文档。

{
_id: 1,
results: [
{
item: 'A',
score: 5,
answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ]
}
]
},
{
_id: 2,
results: [
{
item: 'C',
score: 8,
answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ]
}
]
}

提示

← $pop
$push →