$pull

On this page本页内容

$pull

The $pull operator removes from an existing array all instances of a value or values that match a specified condition.$pull运算符从现有数组中删除与指定条件匹配的一个或多个值的所有实例。

The $pull operator has the form:$pull运算符的形式如下:

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

To specify a <field> in an embedded document or in an array, use dot notation.若要在嵌入式文档或数组中指定一个<field>,请使用点表示法

Behavior行为

If you specify a <condition> and the array elements are embedded documents, $pull operator applies the <condition> as if each array element were a document in a collection. 如果指定一个<condition>,并且数组元素是嵌入的文档,$pull运算符应用<condition>,就像每个数组元素是集合中的文档一样。See Remove Items from an Array of Documents for an example.有关示例,请参阅从文档数组中删除项

If the specified <value> to remove is an array, $pull removes only the elements in the array that match the specified <value> exactly, including order.如果指定要删除的<value>是数组,$pull只删除数组中与指定<value>完全匹配的元素,包括顺序。

If the specified <value> to remove is a document, $pull removes only the elements in the array that have the exact same fields and values. 如果指定要删除的<value>是文档,$pull只删除数组中具有完全相同字段和值的元素。The ordering of the fields can differ.字段的顺序可能会有所不同。

Examples示例

Remove All Items That Equal a Specified Value删除所有等于指定值的项

Given the following document in the stores collection:鉴于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" ]
}

The following operation updates all documents in the collection to remove "apples" and "oranges" from the array fruits and remove "carrots" from the array vegetables:以下操作将更新集合中的所有文档,以从数组fruits中移除"apples""oranges",并从数组vegetables中移除"carrots"

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

After the operation, the fruits array no longer contains any "apples" or "oranges" values, and the vegetables array no longer contains any "carrots" values:操作完成后,fruits数组不再包含任何"apples""oranges"值,vegetables数组不再包含任何"carrots"值:

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

Remove All Items That Match a Specified $pull Condition移除所有符合指定$pull条件的项目

Given the following document in the profiles collection:profiles集合中提供以下文档:

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

The following operation will remove all items from the votes array that are greater than or equal to ($gte) 6:以下操作将从投票数组中删除大于或等于($gte6的所有项目:

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

After the update operation, the document only has values less than 6:更新操作后,文档的值仅小于6:

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

Remove Items from an Array of Documents从文档数组中删除项

A survey collection contains the following documents: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 }
   ]
}

The following operation will remove from the results array all elements that contain both a score field equal to 8 and an item field equal to "B":以下操作将从results数组中删除包含等于8score字段和等于"B"item字段的所有元素:

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

The $pull expression applies the condition to each element of the results array as though it were a top-level document.$pull表达式将条件应用于results数组的每个元素,就像它是顶级文档一样。

After the operation, the results array contains no documents that contain both a score field equal to 8 and an item field equal to "B".操作完成后,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 }
   ]
}

Because $pull operator applies its query to each element as though it were a top-level object, the expression did not require the use of $elemMatch to specify the condition of a score field equal to 8 and item field equal to "B". 由于$pull运算符将其查询应用于每个元素,就像它是顶级对象一样,因此表达式不需要使用$elemMatch来指定score字段等于8item字段等于"B"的条件。In fact, the following operation will not pull any element from the original collection.实际上,以下操作不会从原始集合中提取任何元素。

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

However, if the survey collection contained the following documents, where the results array contains embedded documents that also contain arrays:但是,如果调查集合包含以下文档,其中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 } ] }
   ]
}

Then you can specify multiple conditions on the elements of the answers array with $elemMatch:然后可以使用$elemMatchanswers数组的元素上指定多个条件:

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

The operation removed from the results array those embedded documents with an answers field that contained at least one element with q equal to 2 and a greater than or equal to 8:该操作从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 } ] }
   ]
}