mongodb - 如何在聚合命令期间跳过前 n 个批处理文档?
问题描述
我运行这个命令:
db.runCommand(
{
aggregate:"myColl",
pipeline:[
{
$group:{
_id:{_id:"$_id"},
count:{$sum:NumberInt(1)}}
}
],
cursor:{}
}
)
它给我的回报是:
{
"cursor" : {
"firstBatch" : [
{
"_id" : {
"_id" : NumberLong(-9223365602644598416)
},
"count" : 1.0
},
{
"_id" : {
"_id" : NumberLong(-9223363178405875157)
},
"count" : 1.0
},
{
"_id" : {
"_id" : NumberLong(-9223350896770545240)
},
"count" : 1.0
},
{
"_id" : {
"_id" : NumberLong(-9223338355283447904)
},
"count" : 1.0
},
....
],
"id" : NumberLong(4379399839731469797),
"ns" : "myDatabase.myColl"
},
"ok" : 1.0
}
现在我想向命令指示要跳过的批处理文档的数量以及要返回的批处理文档的数量。可能吗?
解决方案
你可以试试这个:
db.runCommand(
{
aggregate: "myColl",
pipeline: [
{
$group: {
_id: { _id: "$_id" },
count: { $sum: NumberInt(1) }
}
},
{ $skip: n } // n should be a positive integer, where n documents are skipped
],
cursor: { "batchSize": n } // n should be a number, it denotes the batch size.
}
)
推荐阅读
- visual-studio - 在 Windows 10 Visual Studio 2017 上从 cmake 构建 OpenCV 编译错误
- python - 类型错误:promovePeao() 缺少 1 个必需的位置参数:“玩家”
- symfony - Symfony 2.7 自定义异常处理程序
- c# - 从gridview内部的datatemplate访问控制
- cygwin - 在 cygwin32 中使用 mingw32 glfw3 和 glew32s 二进制文件,而不将 cygwin X11 GLX 与 WGL 混合
- azure-cloud-services - 是否有每隔一段时间重新启动一个 Azure 经典云服务角色?
- javascript - SweetAlert 不等待
- java - 当我在其 ViewPort 中绘制 JPanel 时,JScrollPane 不滚动
- python - 将文本写入单行字符串之间
- javascript - 将字符串解释为转义字符串