$push
On this page本页内容
Definition定义
Behavior行为
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. 从MongoDB 5.0开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
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 a Value to Arrays in Multiple Documents. 有关示例,请参阅将值附加到多个文档中的数组。For a list of modifiers available for 有关$push
, see Modifiers.$push
可用的修改器列表,请参阅修饰符。
Starting in MongoDB 5.0, 从MongoDB 5.0开始,当您将mongod
no longer raises an error when you use an update operator like $push
with an empty operand expression ( { }
). $push
之类的更新运算符与空操作数表达式({ }
)一起使用时,mongod
不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。
Modifiers修饰符
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
运算符的形式如下:
{ $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
操作的处理按以下顺序进行,而与修饰符的出现顺序无关:
Update array to add elements in the correct position.更新数组以将元素添加到正确的位置。Apply sort, if specified.应用排序(如果指定)。Slice the array, if specified.如果指定,请对数组进行分片。Store the array.存储数组。
Examples实例
Create the 创建students
collection:students
集合:
db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )
Append a Value to an Array将值附加到数组
The following example appends 以下示例将89
to the scores
array:89
添加到scores
数组中:
db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
Example output:示例输出:
{ _id: 1, scores: [ 44, 78, 38, 80, 89 ] }
Append a Value to Arrays in Multiple Documents将值附加到多个文档中的数组
Add the following documents to the 将以下文档添加到students
collection:students
集合中:
db.students.insertMany( [
{ _id: 2, scores: [ 45, 78, 38, 80, 89 ] } ,
{ _id: 3, scores: [ 46, 78, 38, 80, 89 ] } ,
{ _id: 4, scores: [ 47, 78, 38, 80, 89 ] }
] )
The following 以下$push
operation appends 95
to the scores
array in each document:$push
操作将95
添加到每个文档中的scores
数组中:
db.students.updateMany(
{ },
{ $push: { scores: 95 } }
)
To confirm that each 要确认每个scores
array includes 95
, run the following operation:scores
数组包含95
,请运行以下操作:
db.students.find()
The operation returns the following results:该操作返回以下结果:
[
{ _id: 1, scores: [ 44, 78, 38, 80, 89, 95 ] },
{ _id: 2, scores: [ 45, 78, 38, 80, 89, 95 ] },
{ _id: 3, scores: [ 46, 78, 38, 80, 89, 95 ] },
{ _id: 4, scores: [ 47, 78, 38, 80, 89, 95 ] }
]
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 ]
的每个元素附加到文档的分数数组中,其中name
字段等于joe
:
db.students.updateOne(
{ name: "joe" },
{ $push: { scores: { $each: [ 90, 92, 85 ] } } }
)
Use $push
Operator with Multiple Modifiers将$push
运算符与多个修饰符一起使用
$push
Operator with Multiple ModifiersAdd the following document to the 将以下文档添加到students
collection:students
集合中:
db.students.insertOne(
{
"_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 thequizzes
array,$each
修饰符将多个文档添加到quizzes
数组,the$sort
modifier to sort all the elements of the modifiedquizzes
array by thescore
field in descending order, and$sort
修饰符,用于按score
字段降序对修改后的测验数组的所有元素进行排序,以及the$slice
modifier to keep only the first three sorted elements of thequizzes
array.$slice
修饰符只保留quizzes
数组的前三个排序元素。
db.students.updateOne(
{ _id: 5 },
{
$push: {
quizzes: {
$each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
$sort: { score: -1 },
$slice: 3
}
}
}
)
After the operation only the three highest scoring quizzes are in the array:手术后,数组中只有三个得分最高的测验:
{
"_id" : 5,
"quizzes" : [
{ "wk" : 1, "score" : 10 },
{ "wk" : 2, "score" : 8 },
{ "wk" : 5, "score" : 8 }
]
}