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 the
$each modifier with
$push. For an example, see Append Multiple Values to an Array. For a list of modifiers available for
$push, see Modifiers.
You can use the
$push operator with the following modifiers:
Appends multiple values to the array field.
When used in conjunction with the other modifiers, the
||Limits the number of array elements. Requires the use of the
||Orders elements of the array. Requires the use of the
||Specifies the location in the array at which to insert the new elements. Requires the use of the
When used with modifiers, the
$push operator has the form:
The processing of the
push operation with modifiers occur in the following order, regardless of the order in which the modifiers appear:
- Update array to add elements in the correct position.
- Apply sort, if specified.
- Slice the array, if specified.
- Store the array.
The following example appends each element of
[ 90, 92, 85 ] to the
scores array for the document where the
name field equals
students has the following document:
$push operation uses:
$eachmodifier to add multiple documents to the
$sortmodifier to sort all the elements of the modified
quizzesarray by the
scorefield in descending order, and
$slicemodifier to keep only the first three sorted elements of the
The result of the operation is keep only the three highest scoring quizzes: