OPTIONS
翻译或纠错本页面

$push

$push

The $push operator appends a specified value to an array.

db.collection.update( <query>,
                      { $push: { <field>: <value> } }
                   )

The following example appends 89 to the scores array for the first document where the _id field equals 1:

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

注解

  • If the field is absent in the document to update, $push adds the array field with the value as its element.

  • 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. To add each element of the value separately, use $push with the $each modifier.

    在 2.4 版更改: MongoDB adds support for the $each modifier to the $push operator. Before 2.4, use $pushAll for similar functionality.

    The following example appends each element of [ 90, 92, 85 ] to the scores array for the document where the name field equals joe:

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

Modifiers

2.4 新版功能.

You can use the $push operator with the following modifiers:

  • $each appends multiple values to the array field,

    在 2.6 版更改: When used in conjunction with the other modifiers, the $each modifier no longer needs to be first.

  • $slice, which is only available when used with $each, limits the number of array elements,

  • $sort, which is only available when used with $each, orders elements of the array, and

    在 2.6 版更改: In previous versions, $sort is only available when used with both $each and $slice.

  • $position, which is only available when used with $each, specifies the location in the array at which to insert the new elements. Without the $position modifier, the $push appends the elements to the end of the array.

    2.6 新版功能.

The processing of the push operation with modifiers occur in the following order, regardless of the order in which the modifiers appear:

  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

Use $push Operator with Multiple Modifiers

A collection students has the following document:

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

The following $push operation uses:

  • the $each modifier to add multiple documents to the quizzes array,
  • the $sort modifier to sort all the elements of the modified quizzes array by the score field in descending order, and
  • the $slice modifier to keep only the first three sorted elements of the quizzes array.
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 }
              ]
}
←   $pushAll $each  →