$slice (projection)
On this page本页内容
Definition定义
$slice
-
The$slice
projection operator specifies the number of elements in an array to return in the query result.$slice
投影运算符指定数组中要在查询结果中返回的元素数。
Syntax语法
The $slice
has one of the following syntax forms:$slice
具有以下语法形式之一:
db.collection.find(
<query>,
{ <arrayField>: { $slice: <number> } }
);
or
db.collection.find(
<query>,
{ <arrayField>: { $slice: [ <number>, <number> ] } }
);
$slice: <number> | <arrayField> . <arrayField> 中返回的元素数。<number> : <number> :
<number> is greater than the number of array elements, the query returns all array elements. <number> 大于数组元素的数量,则查询将返回所有数组元素。 |
$slice: [ <number to skip>, <number to return> ] | <arrayField> after skipping the specified number of elements starting from the first element. <arrayField> 中返回的元素数量。<number to skip> : <number to skip> :
<number to return> , you must specify a positive number n to return the next n elements, starting after skipping the specified number. <number to return> ,您必须指定一个正数n 来返回接下来的n 个元素,从跳过指定的数字开始。 |
Behavior行为
$slice
of Embedded Array嵌入式数组的$slice
$slice
of Embedded ArrayStarting in MongoDB 4.4, the 从MongoDB 4.4开始,嵌套文档中数组的$slice
projection of an array in an nested document no longer returns the other fields in the nested document when the projection is part of an inclusion projection.$slice
投影不再返回嵌套文档中的其他字段,因为该投影是包含投影的一部分。
For example, consider a collection 例如,考虑具有包含inventory
with documents that contain a size
field:size
字段的文档的集合inventory
:
{ item: "socks", qty: 100, details: { colors: [ "blue", "red" ], sizes: [ "S", "M", "L"] } }
Starting in MongoDB 4.4, the following operation projects the 从MongoDB 4.4开始,以下操作将_id
field (by default), the qty
field, and the details
field with just the specified slice of the colors
array:_id
字段(默认情况下)、qty
字段和details
字段与colors
数组的指定分片一起投影:
db.inventory.find( { }, { qty: 1, "details.colors": { $slice: 1 } } )
That is, the operation returns the following document:也就是说,该操作返回以下文档:
{ "_id" : ObjectId("5ee92a6ec644acb6d13eedb1"), "qty" : 100, "details" : { "colors" : [ "blue" ] } }
If the 如果$slice
projection is part of an exclusion projection, the operation continues to return the other fields in the nested document. $slice
投影是排除投影的一部分,则操作将继续返回嵌套文档中的其他字段。That is, the following projection is an exclusion projection. 也就是说,以下投影是排除投影。The projection excludes the 投影排除_id
field and the elements in the colors
array that fall outside the specified slice and returns all other fields._id
字段和colors
数组中位于指定分片之外的元素,并返回所有其他字段。
db.inventory.find( { }, { _id: 0, "details.colors": { $slice: 1 } } )
{ "item" : "socks", "qty" : 100, "details" : { "colors" : [ "blue" ], "sizes" : [ "S", "M", "L" ] } }
The $slice
projection by itself is considered an exclusion.$slice
投影本身被认为是一种排除。
In previous versions, the 在以前的版本中,$slice
projection also include the other fields in the nested document regardless of whether the projection is an inclusion or an exclusion.$slice
投影还包括嵌套文档中的其他字段,无论该投影是包含还是排除。
View Restriction查看限制
视图上的db.collection.find()
operations on views do not support $slice
projection operator.db.collection.find()
操作不支持$slice
投影运算符。
$
Positional Operator and $slice
Restriction$
位置运算符和$slice
限制
$
Positional Operator and $slice
RestrictionStarting in MongoDB 4.4, 从MongoDB 4.4开始,find
and findAndModify
projection cannot include $slice
projection expression as part of a $
projection expression.find
和findAndModify
投影不能包含$slice
投影表达式作为$
投影表达式的一部分。
For example, starting in MongoDB 4.4, the following operation is invalid:例如,在MongoDB 4.4中启动时,以下操作无效:
db.inventory.find( { "instock.qty": { $gt: 25 } }, { "instock.$": { $slice: 1 } } ) // Invalid starting in 4.4
In previous versions, MongoDB returns the first element (在以前的版本中,MongoDB返回instock.$
) in the instock
array that matches the query condition; i.e. the positional projection "instock.$"
takes precedence and the $slice:1
is a no-op. instock
数组中与查询条件匹配的第一个元素(instock.$
);即,位置投影"instock.$"
优先,$slice:1
是no-op。The "instock.$": { $slice: 1 }
does not exclude any other document field."instock.$": { $slice: 1 }
不排除任何其他文档字段。
Path Collision: $slice
of an Array and Embedded Fields路径冲突:数组和嵌入字段的$slice
$slice
of an Array and Embedded FieldsStarting in MongoDB 4.4, 从MongoDB 4.4开始,find
and findAndModify
projection cannot contain both a $slice
of an array and a field embedded in the array.find
和findAndModify
投影不能同时包含数组的$slice
和数组中嵌入的字段。
For example, consider a collection 例如,考虑一个包含数组字段inventory
that contains an array field instock
:instock
的集合inventory
:
{ ..., instock: [ { warehouse: "A", qty: 35 }, { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ], ... }
Starting in MongoDB 4.4, the following operation fails with a 从MongoDB 4.4开始,以下操作失败,出现Path collision
error:Path collision
错误:
db.inventory.find( {}, { "instock": { $slice: 1 }, "instock.warehouse": 0 } ) // Invalid starting in 4.4
In previous versions, the projection applies both projections and returns the first element (在以前的版本中,投影应用两个投影并返回$slice: 1
) in the instock
array but suppresses the warehouse
field in the projected element. instock
数组中的第一个元素($slice:1
),但抑制投影元素中的仓库字段。Starting in MongoDB 4.4, to achieve the same result, use the 从MongoDB 4.4开始,要获得相同的结果,请使用带有两个独立db.collection.aggregate()
method with two separate $project
stages.$project
阶段的db.collection.aggregate()
方法。
See also: 另请参阅:
Examples实例
Create an example collection 使用以下文档创建示例集合posts
with the following documents:posts
:
db.posts.insertMany([
{
_id: 1,
title: "Bagels are not croissants.",
comments: [ { comment: "0. true" }, { comment: "1. croissants aren't bagels."} ]
},
{
_id: 2,
title: "Coffee please.",
comments: [ { comment: "0. fooey" }, { comment: "1. tea please" }, { comment: "2. iced coffee" }, { comment: "3. cappuccino" }, { comment: "4. whatever" } ]
}
])
Return an Array with Its First 3 Elements返回包含前3个元素的数组
The following operation uses the 下面的操作使用$slice
projection operator on the comments
array to return the array with its first three elements. comments
数组上的$slice
投影运算符来返回带有前三个元素的数组。If the array has less than three elements, all elements in the array are returned.如果数组的元素少于三个,则返回数组中的所有元素。
db.posts.find( {}, { comments: { $slice: 3 } } )
The operation returns the following documents:该操作返回以下文档:
{
"_id" : 1,
"title" : "Bagels are not croissants.",
"comments" : [ { "comment" : "0. true" }, { "comment" : "1. croissants aren't bagels." } ]
}
{
"_id" : 2,
"title" : "Coffee please.",
"comments" : [ { "comment" : "0. fooey" }, { "comment" : "1. tea please" }, { "comment" : "2. iced coffee" } ]
}
Return an Array with Its Last 3 Elements返回带有最后3个元素的数组
The following operation uses the 下面的操作使用$slice
projection operator on the comments
array to return the array with its last three elements. comments
数组上的$slice
投影运算符来返回带有最后三个元素的数组。If the array has less than three elements, all elements in the array are returned.如果数组的元素少于三个,则返回数组中的所有元素。
db.posts.find( {}, { comments: { $slice: -3 } } )
The operation returns the following documents:该操作返回以下文档:
{
"_id" : 1,
"title" : "Bagels are not croissants.",
"comments" : [ { "comment" : "0. true" }, { "comment" : "1. croissants aren't bagels." } ]
}
{
"_id" : 2,
"title" : "Coffee please.",
"comments" : [ { "comment" : "2. iced coffee" }, { "comment" : "3. cappuccino" }, { "comment" : "4. whatever" } ]
}
Return an Array with 3 Elements After Skipping the First Element跳过第一个元素后返回一个包含3个元素的数组
The following operation uses the 以下操作使用$slice
projection operator on the comments
array to:comments
数组上的$slice
投影运算符:
Skip the first element such that the second element is the starting point.跳过第一个元素,使第二个元素成为起点。Then, return three elements from the starting point.然后,从起点返回三个元素。
If the array has less than three elements after the skip, all remaining elements are returned.如果跳过后数组的元素少于三个,则返回所有剩余元素。
db.posts.find( {}, { comments: { $slice: [ 1, 3 ] } } )
The operation returns the following documents:该操作返回以下文档:
{
"_id" : 1,
"title" : "Bagels are not croissants.",
"comments" : [ { "comment" : "1. croissants aren't bagels." } ]
}
{
"_id" : 2,
"title" : "Coffee please.",
"comments" : [ { "comment" : "1. tea please" }, { "comment" : "2. iced coffee" }, { "comment" : "3. cappuccino" } ]
}
Return an Array with 3 Elements After Skipping the Last Element跳至最后一个元素后返回一个包含3个元素的数组
The following operation uses the 以下操作使用$slice
projection operator on the comments
array tocomments
数组上的$slice
投影运算符以实现
Skip backwards from the first element such that the last element is the starting point.从第一个元素向后跳过,使最后一个元素成为起点。Then, return three elements from the starting point.然后,从起点返回三个元素。
If the array has less than three elements after the skip, all remaining elements in the array are returned.如果跳过后数组的元素少于三个,则返回数组中的所有剩余元素。
db.posts.find( {}, { comments: { $slice: [ -1, 3 ] } } )
The operation returns the following documents:该操作返回以下文档:
{
"_id" : 1,
"title" : "Bagels are not croissants.",
"comments" : [ { "comment" : "1. croissants aren't bagels." } ]
}
{
"_id" : 2,
"title" : "Coffee please.",
"comments" : [ { "comment" : "4. whatever" } ]
}