$push

On this page本页内容

Definition定义

$push

The $push operator appends a specified value to an array.$push运算符将指定的值附加到数组中。

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

{ $push: { <field1>: <value1>, ... } }

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

Behavior行为

If the field is absent in the document to update, $push adds the array field with the value as its element.如果要更新的文档中没有该字段,$push会将该值作为其元素添加到数组字段中。

If the field is not an array, the operation will fail.如果字段不是数组,则操作将失败。

If the value is an array, $push appends the whole array as a single element. 如果值是数组,$push将整个数组作为单个元素追加。To add each element of the value separately, use the $each modifier with $push. 要单独添加值的每个元素,请将$each修饰符与$push一起使用。For an example, see Append Multiple Values to an Array. 有关示例,请参阅将多个值附加到数组For a list of modifiers available for $push, see Modifiers.有关$push可用的修饰符列表,请参阅修饰符

Modifiers修饰符

You can use the $push operator with the following modifiers:可以将$push运算符与以下修饰符一起使用:

Modifier修饰符Description描述
$each Appends multiple values to the array field.将多个值附加到数组字段。
$slice Limits the number of array elements. 限制数组元素的数量。Requires the use of the $each modifier.需要使用$each修饰符。
$sort Orders elements of the array. 对数组中的元素进行排序。Requires the use of the $each modifier.需要使用$each修饰符。
$position Specifies the location in the array at which to insert the new elements. 指定数组中插入新元素的位置。Requires the use of the $each modifier. 需要使用$each修饰符。Without the $position modifier, the $push appends the elements to the end of the array.如果没有$position修饰符,$push会将元素附加到数组的末尾。

When used with modifiers, the $push operator has the form:与修饰符一起使用时,$push运算符的形式如下:

{ $push: { <field1>: { <modifier1>: <value1>, ... }, ... } }

The processing of the push operation with modifiers occur in the following order, regardless of the order in which the modifiers appear:无论修改器的显示顺序如何,使用修饰符的push操作的处理顺序如下:

  1. Update array to add elements in the correct position.更新数组以在正确位置添加元素。
  2. Apply sort, if specified.应用排序(如果指定)。
  3. Slice the array, if specified.如果指定,则对数组进行切片。
  4. Store the array.存储阵列。

Examples示例

Append a Value to an Array将值附加到数组中

The following example appends 89 to the scores array:以下示例将89添加到scores数组中:

db.students.update(
   { _id: 1 },
   { $push: { scores: 89 } }
)

Append Multiple Values to an Array将多个值附加到数组中

Use $push with the $each modifier to append multiple values to the array field.使用$push$each修饰符将多个值附加到数组字段。

The following example appends each element of [ 90, 92, 85 ] to the scores array for the document where the name field equals joe:下面的示例将[90,92,85]中的每个元素添加到文档的scores数组中,其中name字段等于joe

db.students.update(
   { name: "joe" },
   { $push: { scores: { $each: [ 90, 92, 85 ] } } }
)

Use $push Operator with Multiple Modifiers$push运算符与多个修改器一起使用

A collection students has the following document:集合students具有以下文档:

{
   "_id" : 5,
   "quizzes" : [
      { "wk": 1, "score" : 10 },
      { "wk": 2, "score" : 8 },
      { "wk": 3, "score" : 5 },
      { "wk": 4, "score" : 6 }
   ]
}

The following $push operation uses:以下$push操作使用:

  • the $each modifier to add multiple documents to the quizzes array,$each修饰符将多个文档添加到quizzes数组中,
  • the $sort modifier to sort all the elements of the modified quizzes array by the score field in descending order, and$sort修饰符,用于按score字段降序排列修改后的quizzes数组的所有元素,并且
  • the $slice modifier to keep only the first three sorted elements of the quizzes array.$slice修饰符只保留quizzes数组的三个排序元素。
db.students.update(
   { _id: 5 },
   {
     $push: {
       quizzes: {
          $each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
          $sort: { score: -1 },
          $slice: 3
       }
     }
   }
)

The result of the operation is keep only the three highest scoring quizzes:该操作的结果是只保留三个得分最高的测验:

{
  "_id" : 5,
  "quizzes" : [
     { "wk" : 1, "score" : 10 },
     { "wk" : 2, "score" : 8 },
     { "wk" : 5, "score" : 8 }
  ]
}