$pull

在本页面

$pull运算符的格式为:

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

要在嵌入式文档或数组中指定<field>,请使用dot notation

Behavior

如果指定<condition>并且数组元素是嵌入式文档,则$pull运算符将<condition>应用于每个数组元素都是集合中的文档。有关示例,请参见从一系列文档中删除项目

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

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

Examples

删除所有等于指定值的项目

鉴于stores集合中的以下文档:

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

以下操作更新集合中的所有文档,以从数组fruits中删除"apples""oranges",并从数组vegetables中删除"carrots"

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

操作之后,fruits数组不再包含任何"apples""oranges"值,并且vegetables数组不再包含任何"carrots"值:

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

删除所有符合指定$ pull 条件的项目

鉴于profiles集合中的以下文档:

{ _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }

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

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

更新操作后,文档的值仅小于 6:

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

从一系列文档中删除项目

survey集合包含以下文档:

{
   _id: 1,
   results: [
      { item: "A", score: 5 },
      { item: "B", score: 8, comment: "Strongly agree" }
   ]
}
{
   _id: 2,
   results: [
      { item: "C", score: 8, comment: "Strongly agree" },
      { item: "B", score: 4 }
   ]
}

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

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

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

操作之后,results数组不包含同时包含等于8score字段和等于"B"item字段的文档。

{
   "_id" : 1,
   "results" : [ { "item" : "A", "score" : 5 } ]
}
{
  "_id" : 2,
  "results" : [
      { "item" : "C", "score" : 8, "comment" : "Strongly agree" },
      { "item" : "B", "score" : 4 }
   ]
}

由于$pull运算符将其查询应用于每个元素就好像它是顶级对象一样,因此该表达式不需要使用$elemMatch来指定等于8score字段和等于"B"item字段的条件。实际上,以下操作不会从原始集合中提取任何元素。

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

但是,如果survey集合包含以下文档,则results数组包含也包含数组的嵌入式文档:

{
   _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.update(
  { },
  { $pull: { results: { answers: { $elemMatch: { q: 2, a: { $gte: 8 } } } } } },
  { multi: true }
)

该操作从results数组中删除了具有answers字段的嵌入文档,这些文档包含q等于2a大于或等于8的至少一个元素:

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