你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
聚合管道中的 $skip 阶段用于跳过输入中的指定数量的文档,并将其余文档传递到管道中的下一阶段。 该阶段可用于在查询中实现分页,以及控制管道中后续阶段操作的文档子集。
语法
$skip 阶段的语法非常简单。 它接受单个参数,即要跳过的文档数。
{
$skip: <number>
}
参数
DESCRIPTION | |
---|---|
number |
将剩余文档传递到下一阶段之前要跳过的文档数。 |
例子
示例 1:跳过集合中的文档
假设我们有一个名为商店的集合,其中包含表示各种商店详细信息的文档。 若要跳过前 2 个文档并返回其余文档,可以使用以下聚合管道:
db.stores.aggregate([
{ $skip: 2 }
])
示例输出
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"promotionEvents": ["Summer Sale", "Black Friday", "Holiday Deals"]
}
},
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c6",
"store": {
"name": "Uptown Store",
"promotionEvents": ["Back to School", "Winter Sale"]
}
}
]
示例 2:跳过文档,然后限制结果
若要跳过前 2 个文档,然后将结果限制为接下来的 3 个文档,可以将 $skip 与 $limit 组合在一起:
db.stores.aggregate([
{ $skip: 2 },
{ $limit: 3 }
])
示例 3:跳过更复杂的管道中的文档
假设你想要跳过第一个促销活动,然后投影特定商店的剩余活动:
db.stores.aggregate([
{ $match: { "store.storeId": "12345" } },
{ $unwind: "$store.promotionEvents" },
{ $skip: 1 },
{ $project: { "store.promotionEvents": 1, _id: 0 } }
])
相关内容
- 查看从 MongoDB 迁移到 Azure Cosmos DB for MongoDB (vCore) 的选项
- 通过创建帐户开始。