文档菜单
文档首页
/
MongoDB 手册
/ / / /

$slice (投影)

在本页

  • 定义
  • 语法
  • 行为
  • 示例
$slice

$slice 投影运算符指定查询结果中返回的数组元素数量。

注意

区分

有关使用$push 在更新期间限制数组大小的信息,请参阅 $slice 修改器。

有关聚合运算符,请参阅 $slice 聚合运算符。

$slice 具有以下语法形式之一

db.collection.find(
<query>,
{ <arrayField>: { $slice: <number> } }
);

db.collection.find(
<query>,
{ <arrayField>: { $slice: [ <number>, <number> ] } }
);
描述
$slice: <number>

指定在 <arrayField> 中返回的元素数量。对于 <number>

  • 指定正数 n 以返回前 n 个元素。

  • 指定负数 n 以返回最后 n 个元素。

如果 <number> 大于数组元素的数量,查询将返回所有数组元素。

$slice: [ <number to skip>, <number to return> ]

指定在跳过从第一个元素开始的指定数量元素后,在 <arrayField> 中返回的元素数量。您必须指定这两个元素。

对于 <number to skip>

  • 指定一个正数 n 来从数组的开始位置跳过 n 个元素;即第 0 个索引位置。基于以 0 为基数的数组索引,1 表示第二个元素的起始位置,依此类推。如果 n 大于数组元素的数量,查询将返回一个空的 <arrayField> 数组。

  • 指定一个负数 n 来从数组的开始位置向后跳过 n 个元素;即第 0 个索引位置。基于以 0 为基数的数组索引(即第一个元素在索引 0),-1 表示最后一个元素的起始位置,依此类推。如果负数的绝对值大于数组元素的数量,起始位置为数组的开始位置。

对于 <number to return>,您必须指定一个 正数 n 来返回在跳过后指定的下一个 n 个元素。

嵌套文档中数组 $slice 投影在包含投影的一部分时不再返回嵌套文档中的其他字段。

例如,考虑一个包含 size 字段的 inventory 集合

{ item: "socks", qty: 100, details: { colors: [ "blue", "red" ], sizes: [ "S", "M", "L"] } }

以下操作投影 _id 字段(默认),qty 字段,以及仅指定切片的 details 字段,用于 colors 数组

db.inventory.find( { }, { qty: 1, "details.colors": { $slice: 1 } } )

即,操作返回以下文档

{ "_id" : ObjectId("5ee92a6ec644acb6d13eedb1"), "qty" : 100, "details" : { "colors" : [ "blue" ] } }

如果$slice投影是排除投影的一部分,则操作将继续返回嵌套文档中的其他字段。也就是说,以下投影是一个排除投影。该投影排除了_id字段和落在指定切片之外的颜色数组的元素,并返回所有其他字段。

db.inventory.find( { }, { _id: 0, "details.colors": { $slice: 1 } } )
{ "item" : "socks", "qty" : 100, "details" : { "colors" : [ "blue" ], "sizes" : [ "S", "M", "L" ] } }

$slice投影本身被视为排除。

在之前的版本中,$slice投影也包含嵌套文档中的其他字段,无论投影是包含还是排除。

在视图上执行的db.collection.find()操作不支持$slice投影运算符。

findfindAndModify投影不能将$slice投影表达式作为$投影表达式的一部分。

例如,以下操作是无效的

db.inventory.find( { "instock.qty": { $gt: 25 } }, { "instock.$": { $slice: 1 } } )

在之前的版本中,MongoDB 返回匹配查询条件的 instock 数组中的第一个元素(instock.$);即位置投影 "instock.$" 优先级更高,而 $slice:1 是一个空操作。表达式 "instock.$": { $slice: 1 } 不会排除任何其他文档字段。

findfindAndModify 投影不能同时包含数组的一个 $slice 和数组中嵌入的字段。

例如,考虑一个包含数组字段 instock 的集合 inventory

{ ..., instock: [ { warehouse: "A", qty: 35 }, { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ], ... }

以下操作由于 Path collision 错误而失败

db.inventory.find( {}, { "instock": { $slice: 1 }, "instock.warehouse": 0 } )

在之前的版本中,投影同时应用两个投影并返回 instock 数组中的第一个元素($slice: 1),但抑制了投影元素中的 warehouse 字段。从 MongoDB 4.4 版本开始,为了达到相同的结果,请使用 db.collection.aggregate() 方法并包含两个独立的 $project 阶段。

提示

另请参阅

创建一个包含以下文档的示例集合 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" } ]
}
])

以下操作使用$slice投影运算符在comments数组上,以返回包含前三个元素的数组。如果数组元素少于三个,则返回数组中所有元素。

db.posts.find( {}, { comments: { $slice: 3 } } )

此操作返回以下文档

{
"_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" } ]
}

以下操作使用$slice投影运算符在comments数组上,以返回包含最后三个元素的数组。如果数组元素少于三个,则返回数组中所有元素。

db.posts.find( {}, { comments: { $slice: -3 } } )

此操作返回以下文档

{
"_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" } ]
}

以下操作使用$slice投影运算符对comments数组进行处理,

  • 跳过第一个元素,使得第二个元素为起点。

  • 然后,从起点返回三个元素。

如果跳过后数组中元素少于三个,则返回所有剩余元素。

db.posts.find( {}, { comments: { $slice: [ 1, 3 ] } } )

此操作返回以下文档

{
"_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" } ]
}

以下操作使用$slice投影运算符对comments数组进行处理,

  • 从第一个元素开始向后跳过,使得最后一个元素为起点。

  • 然后,从起点返回三个元素。

如果跳过后数组中元素少于三个,则返回数组中的所有剩余元素。

db.posts.find( {}, { comments: { $slice: [ -1, 3 ] } } )

此操作返回以下文档

{
"_id" : 1,
"title" : "Bagels are not croissants.",
"comments" : [ { "comment" : "1. croissants aren't bagels." } ]
}
{
"_id" : 2,
"title" : "Coffee please.",
"comments" : [ { "comment" : "4. whatever" } ]
}

返回

$elemMatch