$slice (aggregation)

On this page本页内容

Definition定义

$slice

New in version 3.2.版本3.2中的新功能。

Returns a subset of an array.返回数组的子集。

$slice has one of two syntax forms:$slice有两种语法形式之一:

The following syntax returns elements from either the start or end of the array:以下语法返回数组开头或结尾的元素:

{ $slice: [ <array>, <n> ] }

The following syntax returns elements from the specified position in the array:以下语法返回数组中指定位置的元素:

{ $slice: [ <array>, <position>, <n> ] }
Operand运算数Description描述
<array> Any valid expression as long as it resolves to an array.任何有效的表达式,只要它解析为数组。
<position>

Optional.可选。Any valid expression as long as it resolves to an integer.任何有效的表达式,只要它解析为整数。

  • If positive, $slice determines the starting position from the start of the array. 如果为正,$slice确定从数组开始的起始位置。If <position> is greater than the number of elements, the $slice returns an empty array.如果<position>大于元素数,$slice将返回一个空数组。
  • If negative, $slice determines the starting position from the end of the array. 如果为负,$slice确定从数组末尾开始的位置。If the absolute value of the <position> is greater than the number of elements, the starting position is the start of the array.如果<position>的绝对值大于元素数,则起始位置为数组的起始位置。
<n>

Any valid expression as long as it resolves to an integer. 任何有效的表达式,只要它解析为整数。If <position> is specified, <n> must resolve to a positive integer.如果指定了<position><n>必须解析为正整数。

  • If positive, $slice returns up to the first n elements in the array. 如果为正,$slice返回数组中的前n个元素。If the <position> is specified, $slice returns the first n elements starting from the position.如果指定了<position>$slice返回从该位置开始的前n个元素。
  • If negative, $slice returns up to the last n elements in the array. 如果为负,$slice返回数组中的最后n个元素。n cannot resolve to a negative number if <position> is specified.如果指定了<position>n不能解析为负数。

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

Example示例Results结果
{ $slice: [ [ 1, 2, 3 ], 1, 1 ] }
[ 2 ]
{ $slice: [ [ 1, 2, 3 ], -2 ] }
[ 2, 3 ]
{ $slice: [ [ 1, 2, 3 ], 15, 2 ] }
[  ]
{ $slice: [ [ 1, 2, 3 ], -15, 2 ] }
[ 1, 2 ]

Example示例

A collection named users contains the following documents:名为users的集合包含以下文档:

{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }

The following example returns at most the first three elements in the favorites array for each user:以下示例最多为每个用户返回favorites数组中的前三个元素:

db.users.aggregate([
   { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } }
])

The operation returns the following results:操作返回以下结果:

{ "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] }
{ "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] }
{ "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }