$position
On this page本页内容
Definition定义
$position
-
The$position
modifier specifies the location in the array at which the$push
operator inserts elements.$position
修饰符指定$push
运算符在数组中插入元素的位置。Without the如果没有$position
modifier, the$push
operator inserts elements to the end of the array.$position
修饰符,$push
运算符会将元素插入到数组的末尾。See $push modifiers for more information.有关详细信息,请参阅$push
修饰符。To use the要使用$position
modifier, it must appear with the$each
modifier.$position
修饰符,它必须与$each
修饰符一起出现。{
$push: {
<field>: {
$each: [ <value1>, <value2>, ... ],
$position: <num>
}
}
}<num>
indicates the position in the array, based on a zero-based array index (position):<num>
表示数组中的位置,基于从零开始的数组索引(位置):A non-negative number corresponds to the position in the array, starting from the beginning of the array.非负数对应于数组中的位置,从数组的开头开始。If the value of如果<num>
is greater or equal to the length of the array, the$position
modifier has no effect and$push
adds elements to the end of the array.<num>
的值大于或等于数组的长度,则$position
修饰符无效,$push
将元素添加到数组的末尾。A negative number corresponds to the position in the array, counting from (but not including) the last element of the array.负数对应于数组中的位置,从数组的最后一个元素开始计数(但不包括)。For example,例如,-1
indicates the position just before the last element in the array.-1
表示数组中最后一个元素之前的位置。If you specify multiple elements in the如果在$each
array, the last added element is in the specified position from the end.$each
数组中指定多个元素,则最后添加的元素位于从末尾开始的指定位置。If the absolute value of如果<num>
is greater than or equal to the length of the array, the$push
adds elements to the beginning of the array.<num>
的绝对值大于或等于数组的长度,$push
会将元素添加到数组的开头。
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开始,更新运算符按照字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
Examples实例
Add Elements at the Start of the Array在数组的开头添加元素
Create the 创建students
collection:students
集合:
db.students.insertOne( { "_id" : 1, "scores" : [ 100 ] } )
The following operation updates the 以下操作更新scores
field to add the elements 50
, 60
and 70
to the beginning of the array:scores
字段,将元素50
、60
和70
添加到数组的开头:
db.students.updateOne(
{ _id: 1 },
{
$push: {
scores: {
$each: [ 50, 60, 70 ],
$position: 0
}
}
}
)
The operation results in the following updated document:该操作生成以下更新文档:
{ "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
Add Elements to the Middle of the Array将元素添加到数组的中间
Add a document to the 将文档添加到students
collection:students
集合:
db.students.insertOne( { "_id" : 2, "scores" : [ 50, 60, 70, 100 ] } )
The following operation updates the 以下操作更新scores
field to add the elements 20
and 30
at the array index (position) of 2
:scores
字段以在数组索引(位置)为2处添加元素20
和30
:
db.students.updateOne(
{ _id: 2 },
{
$push: {
scores: {
$each: [ 20, 30 ],
$position: 2
}
}
}
)
The operation results in the following updated document:该操作生成以下更新文档:
{ "_id" : 2, "scores" : [ 50, 60, 20, 30, 70, 100 ] }
Use a Negative Array Index (Position) to Add Elements to the Array使用负数组索引(位置)将元素添加到数组
$position
can accept a negative array index (position) value to indicate the position starting from the end, counting from (but not including) the last element of the array. 可以接受负数组索引(位置)值,以指示从末尾开始的位置,从数组的最后一个元素开始计数(但不包括)。For example, 例如,-1
indicates the position just before the last element in the array.-1
表示数组中最后一个元素之前的位置。
Add the following document to the 将以下文档添加到students
collection:students
集合中:
db.students.insertOne(
{ "_id" : 3, "scores" : [ 50, 60, 20, 30, 70, 100 ] }
)
The following operation specifies 下面的操作为-2
for the $position
to add 90
at the position two places before the last element, and then 80
at the position two places before the last element.$position
指定-2
,在最后一个元素前两位的位置加90
,然后在最后一元素前两位置的位置加80
。
db.students.updateOne(
{ _id: 3 },
{
$push: {
scores: {
$each: [ 90, 80 ],
$position: -2
}
}
}
)
The operation results in the following updated document:该操作生成以下更新文档:
{ "_id" : 3, "scores" : [ 50, 60, 20, 30, 90, 80, 70, 100 ] }