对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

$pull(更新操作符)

$pull

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

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

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

$pull 操作符采用以下形式:

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

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

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

如果指定 <condition>,并且数组元素是嵌入式文档,$pull 操作符将应用 <condition>,就像每个数组元素都是集合中的文档一样。有关示例,请参阅从文档数组中删除项。

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

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

从 MongoDB 5.0 开始,使用带空操作数表达式 ({ }) 的更新操作符(如 $pull)时,mongod 不会再引发错误。空更新不会导致任何变化,也不会创建 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 ] } )

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

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);}

操作后,您可以确认文档中的值仅小于 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 数组中同时包含等于 8 的 score 字段和等于 "B" 的 item 字段的所有元素:

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

$pull 表达式将该条件应用于 results 数组的每个元素,并将其视为顶层文档。

执行此操作后,results 数组中不再有同时包含等于 8 的 score 字段和等于 "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 集合,使用:

db.survey.drop()

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

使用嵌入在嵌套数组中的文档创建新的 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 } ] } ] }] )

要对 answers 数组的元素指定多个条件,请使用 $elemMatch:

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 } ] } ]}
给本页内容打分