On this page本页内容
$slice The $slice projection operator specifies the number of elements in an array to return in the query result.$slice投影运算符指定数组中要在查询结果中返回的元素数。
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> |
|
$slice: [ <number to skip>, <number to return> ] |
|
$slice of Embedded Array$sliceStarting 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:inventory,其中的文档包含size字段:
{ 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字段:
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. That is, the following projection is an exclusion projection. $slice投影是排除投影的一部分,则操作将继续返回嵌套文档中的其他字段。也就是说,下面的投影是排除投影。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投影还包括嵌套文档中的其他字段。
视图上的db.collection.find() operations on views do not support $slice projection operator.db.collection.find()操作不支持$slice投影运算符。
$ Positional Operator and $slice Restriction$位置运算符和$slice限制Starting 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 }不排除任何其他文档字段。
$slice of an Array and Embedded Fields$sliceStarting 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:
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()方法。
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" } ]
}
])
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" } ]
}
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" } ]
}
The following operation uses the 以下操作使用注释数组上的$slice projection operator on the comments array to:$slice投影运算符来:
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" } ]
}
The following operation uses the 以下操作使用注释数组上的$slice projection operator on the comments array to$slice投影运算符
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" } ]
}