On this page本页内容
$arrayElemAt
¶New in version 3.2.版本3.2中的新功能。
Returns the element at the specified array index.返回指定数组索引处的元素。
$arrayElemAt
has the following syntax:语法如下所示:
The <array>
expression can be any valid expression that resolves to an array.<array>
表达式可以是解析为数组的任何有效表达式。
The <idx>
expression can be any valid expression that resolves to an integer:<idx>
表达式可以是解析为整数的任何有效表达式:
<idx>
expression resolves to zero or a positive integer, $arrayElemAt
returns the element at the idx
position, counting from the start of the array.<idx>
表达式解析为零或正整数,$arrayElemAt
返回idx
位置的元素,从数组开始计数。<idx>
expression resolves to a negative integer, $arrayElemAt
returns the element at the idx
position, counting from the end of the array.<idx>
表达式解析为负整数,$arrayElemAt
返回idx
位置的元素,从数组末尾开始计数。If 如果idx
exceeds the array bounds, $arrayElemAt
does not return a result.idx
超过数组边界,$arrayElemAt
不会返回结果。
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } |
1 |
{ $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } |
2 |
{ $arrayElemAt: [ [ 1, 2, 3 ], 15 ] } |
A collection named 名为users
contains the following documents:users
的集合包含以下文档:
The following example returns the first and last element in the 以下示例返回favorites
array:favorites
数组中的第一个和最后一个元素:
The operation returns the following results:操作返回以下结果: