On this page本页内容
$push
¶The $push
operator appends a specified value to an array.$push
运算符将指定的值附加到数组中。
The $push
operator has the form:$push
运算符的形式如下:
To specify a 若要在嵌入式文档或数组中指定一个<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
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
可用的修饰符列表,请参阅修饰符。
You can use the 可以将$push
operator with the following modifiers:$push
运算符与以下修饰符一起使用:
$each |
|
$slice |
$each modifier.$each 修饰符。 |
$sort |
$each modifier.$each 修饰符。 |
$position |
$each modifier. $each 修饰符。$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
运算符的形式如下:
The processing of the 无论修改器的显示顺序如何,使用修饰符的push
operation with modifiers occur in the following order, regardless of the order in which the modifiers appear:push
操作的处理顺序如下:
The following example appends 以下示例将89
to the scores
array:89
添加到scores
数组中:
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
:
$push
Operator with Multiple Modifiers$push
运算符与多个修改器一起使用¶A collection 集合students
has the following document:students
具有以下文档:
The following 以下$push
operation uses:$push
操作使用:
$each
modifier to add multiple documents to the quizzes
array,$each
修饰符将多个文档添加到quizzes
数组中,$sort
modifier to sort all the elements of the modified quizzes
array by the score
field in descending order, and$sort
修饰符,用于按score
字段降序排列修改后的quizzes
数组的所有元素,并且$slice
modifier to keep only the first three sorted elements of the quizzes
array.$slice
修饰符只保留quizzes
数组的前三个排序元素。The result of the operation is keep only the three highest scoring quizzes:该操作的结果是只保留三个得分最高的测验:
See also参阅