$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>
    }
  }
}

Changed in version 3.6.在版本3.6中更改。$position can accept a negative array index value to indicate the position starting from the end, counting from (but not including) the last element of the array.$position可以接受一个负的数组索引值,以指示从数组的最后一个元素开始计数(但不包括)的位置。

<num> indicates the position in the array, based on a zero-based index:根据从零开始的索引指示数组中的位置:

  • 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会将元素添加到数组的开头。

Examples示例

Add Elements at the Start of the Array在数组的开头添加元素

Consider a collection students that contains the following document:考虑一个包含下列文档的集合students

{ "_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字段,将元素506070添加到数组的开头:

db.students.update(
   { _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将元素添加到数组的中间

Consider a collection students that contains the following document:考虑一个包含下列文件的集合students

{ "_id" : 1, "scores" : [  50,  60,  70,  100 ] }

The following operation updates the scores field to add the elements 20 and 30 at the array index of 2:以下操作将更新scores字段,以在数组索引2处添加元素2030

db.students.update(
   { _id: 1 },
   {
     $push: {
        scores: {
           $each: [ 20, 30 ],
           $position: 2
        }
     }
   }
)

The operation results in the following updated document:该操作将生成以下更新的文档:

{ "_id" : 1, "scores" : [  50,  60,  20,  30,  70,  100 ] }

Use a Negative Index to Add Elements to the Array使用负索引向数组中添加元素

Changed in version 3.6:在3.6版中更改:$position can accept a negative array index value to indicate the position starting from the end, counting from (but not including) the last element of the array. $position可以接受一个负的数组索引值,以指示从数组的最后一个元素开始计数(但不包括)的位置。For example, -1 indicates the position just before the last element in the array.例如,-1表示数组中最后一个元素之前的位置。

Consider a collection students that contains the following document:考虑一个集合students包含下列文档:

{ "_id" : 1, "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

Important

With a negative index position, if you specify multiple elements in the $each array, the last added element is in the specified position from the end.对于负索引位置,如果在$each数组中指定多个元素,则最后添加的元素位于从末尾开始的指定位置。

db.students.update(
   { _id: 1 },
   {
     $push: {
        scores: {
           $each: [ 90, 80 ],
           $position: -2
        }
     }
   }
)

The operation results in the following updated document:该操作将生成以下更新的文档:

{ "_id" : 1, "scores" : [ 50, 60, 20, 30, 90, 80, 70, 100 ] }