mongodb的聚合函数的$skip + $limit 方法运用 和 顺序优化。
2016-06-27 10:25
766 查看
转自:http://docs.mongoing.com/manual-zh/core/aggregation-pipeline-optimization.html
如果你的管道中, $skip 后面跟着 $limit ,优化器会把 $limit 移到 $skip 前面,这个时候,$limit 的值会加上 $skip 的个数。
例如,如果管道由以下部分组成:
During the optimization phase, the optimizer transforms the sequence to the following:
对于类似 $sort + $limit 合并 ,例如 $sort + $skip + $limit ,优化器允许你做很多优化。详情请查看 $sort
+ $limit 合并 ,也可以在 $sort + $skip + $limit
顺序 中查看例子。
对于在 分片集合上的聚合 ,优化器可以减少从每个分片返回的文档个数。
1、db.memos.find({})
查询memos文档结果;
2、db.memos.aggregate({$skip:3})
跳过查询结果前三行;
3、db.memos.aggregate({$limit:3})
获取查询结果前三行;
1、db.memos.find({})如下:
{
"_id" : 1.0,
"name" : "sue",
"age" : 19.0,
"type" : 1.0,
"status" : "P",
"favorites" : {
"artist" : "Picasso",
"food" : "pizza"
},
"finished" : [
17.0,
3.0
],
"badges" : [
"blue",
"black"
],
"points" : [
{
"points" : 85.0,
"bonus" : 20.0
},
{
"points" : 85.0,
"bonus" : 10.0
}
]
}
{
"_id" : 6.0,
"name" : "abc",
"age" : 43.0,
"type" : 1.0,
"status" : "A",
"favorites" : {
"food" : "pizza",
"artist" : "Picasso"
},
"finished" : [
18.0,
12.0
],
"badges" : [
"black",
"blue"
],
"points" : [
{
"points" : 78.0,
"bonus" : 8.0
},
{
"points" : 57.0,
"bonus" : 7.0
}
]
}
{
"_id" : 7.0,
"name" : "abc",
"age" : 43.0,
"type" : 1.0,
"status" : "A",
"favorites" : {
"food" : "pizza",
"artist" : "Picasso"
},
"finished" : [
21.0,
14.0
],
"badges" : [
"black",
"blue"
],
"points" : [
{
"points" : 78.0,
"bonus" : 8.0
},
{
"points" : 57.0,
"bonus" : 7.0
}
]
}
{
"_id" : 8.0,
"name" : "sue",
"age" : 49.0,
"type" : 1.0,
"status" : "P",
"favorites" : {
"artist" : "Picasso",
"food" : "pizza"
},
"finished" : [
17.0,
3.0
],
"badges" : [
"blue",
"black"
],
"points" : [
{
"points" : 85.0,
"bonus" : 20.0
},
{
"points" : 85.0,
"bonus" : 10.0
}
]
}
{
"_id" : 9.0,
"name" : "sue",
"age" : 19.0,
"type" : 1.0,
"status" : "P",
"favorites" : {
"artist" : "Picasso",
"food" : "pizza"
},
"finished" : [
17.0,
3.0
],
"badges" : [
"blue",
"black"
],
"points" : [
{
"points" : 85.0,
"bonus" : 20.0
},
{
"points" : 85.0,
"bonus" : 10.0
}
]
}
{
"_id" : 10.0,
"name" : "abc",
"age" : 43.0,
"type" : 1.0,
"status" : "A",
"favorites" : {
"food" : "pizza",
"artist" : "Picasso"
},
"finished" : [
18.0,
12.0
],
"badges" : [
"black",
"blue"
],
"points" : [
{
"points" : 78.0,
"bonus" : 8.0
},
{
"points" : 57.0,
"bonus" : 7.0
}
]
}
{
"_id" : 11.0,
"name" : "abc",
"age" : 43.0,
"type" : 1.0,
"status" : "A",
"favorites" : {
"food" : "pizza",
"artist" : "Picasso"
},
"finished" : [
21.0,
14.0
],
"badges" : [
"black",
"blue"
],
"points" : [
{
"points" : 78.0,
"bonus" : 8.0
},
{
"points" : 57.0,
"bonus" : 7.0
}
]
}
{
"_id" : 12.0,
"name" : "sue",
"age" : 49.0,
"type" : 1.0,
"status" : "P",
"favorites" : {
"artist" : "Picasso",
"food" : "pizza"
},
"finished" : [
17.0,
3.0
],
"badges" : [
"blue",
"black"
],
"points" : [
{
"points" : 85.0,
"bonus" : 20.0
},
{
"points" : 85.0,
"bonus" : 10.0
}
]
}
$skip + $limit 顺序优化
如果你的管道中, $skip 后面跟着 $limit ,优化器会把 $limit 移到 $skip 前面,这个时候,$limit 的值会加上 $skip 的个数。例如,如果管道由以下部分组成:
{ $skip: 10 }, { $limit: 5 }
During the optimization phase, the optimizer transforms the sequence to the following:
{ $limit: 15 }, { $skip: 10 }
对于类似 $sort + $limit 合并 ,例如 $sort + $skip + $limit ,优化器允许你做很多优化。详情请查看 $sort
+ $limit 合并 ,也可以在 $sort + $skip + $limit
顺序 中查看例子。
对于在 分片集合上的聚合 ,优化器可以减少从每个分片返回的文档个数。
1、db.memos.find({})
查询memos文档结果;
2、db.memos.aggregate({$skip:3})
跳过查询结果前三行;
3、db.memos.aggregate({$limit:3})
获取查询结果前三行;
1、db.memos.find({})如下:
{
"_id" : 1.0,
"name" : "sue",
"age" : 19.0,
"type" : 1.0,
"status" : "P",
"favorites" : {
"artist" : "Picasso",
"food" : "pizza"
},
"finished" : [
17.0,
3.0
],
"badges" : [
"blue",
"black"
],
"points" : [
{
"points" : 85.0,
"bonus" : 20.0
},
{
"points" : 85.0,
"bonus" : 10.0
}
]
}
{
"_id" : 6.0,
"name" : "abc",
"age" : 43.0,
"type" : 1.0,
"status" : "A",
"favorites" : {
"food" : "pizza",
"artist" : "Picasso"
},
"finished" : [
18.0,
12.0
],
"badges" : [
"black",
"blue"
],
"points" : [
{
"points" : 78.0,
"bonus" : 8.0
},
{
"points" : 57.0,
"bonus" : 7.0
}
]
}
{
"_id" : 7.0,
"name" : "abc",
"age" : 43.0,
"type" : 1.0,
"status" : "A",
"favorites" : {
"food" : "pizza",
"artist" : "Picasso"
},
"finished" : [
21.0,
14.0
],
"badges" : [
"black",
"blue"
],
"points" : [
{
"points" : 78.0,
"bonus" : 8.0
},
{
"points" : 57.0,
"bonus" : 7.0
}
]
}
{
"_id" : 8.0,
"name" : "sue",
"age" : 49.0,
"type" : 1.0,
"status" : "P",
"favorites" : {
"artist" : "Picasso",
"food" : "pizza"
},
"finished" : [
17.0,
3.0
],
"badges" : [
"blue",
"black"
],
"points" : [
{
"points" : 85.0,
"bonus" : 20.0
},
{
"points" : 85.0,
"bonus" : 10.0
}
]
}
{
"_id" : 9.0,
"name" : "sue",
"age" : 19.0,
"type" : 1.0,
"status" : "P",
"favorites" : {
"artist" : "Picasso",
"food" : "pizza"
},
"finished" : [
17.0,
3.0
],
"badges" : [
"blue",
"black"
],
"points" : [
{
"points" : 85.0,
"bonus" : 20.0
},
{
"points" : 85.0,
"bonus" : 10.0
}
]
}
{
"_id" : 10.0,
"name" : "abc",
"age" : 43.0,
"type" : 1.0,
"status" : "A",
"favorites" : {
"food" : "pizza",
"artist" : "Picasso"
},
"finished" : [
18.0,
12.0
],
"badges" : [
"black",
"blue"
],
"points" : [
{
"points" : 78.0,
"bonus" : 8.0
},
{
"points" : 57.0,
"bonus" : 7.0
}
]
}
{
"_id" : 11.0,
"name" : "abc",
"age" : 43.0,
"type" : 1.0,
"status" : "A",
"favorites" : {
"food" : "pizza",
"artist" : "Picasso"
},
"finished" : [
21.0,
14.0
],
"badges" : [
"black",
"blue"
],
"points" : [
{
"points" : 78.0,
"bonus" : 8.0
},
{
"points" : 57.0,
"bonus" : 7.0
}
]
}
{
"_id" : 12.0,
"name" : "sue",
"age" : 49.0,
"type" : 1.0,
"status" : "P",
"favorites" : {
"artist" : "Picasso",
"food" : "pizza"
},
"finished" : [
17.0,
3.0
],
"badges" : [
"blue",
"black"
],
"points" : [
{
"points" : 85.0,
"bonus" : 20.0
},
{
"points" : 85.0,
"bonus" : 10.0
}
]
}
相关文章推荐
- mongodb的聚合函数的$sort 方法运用 和$sort + $match 顺序优化。
- 分享一个Mongodb PHP封装类
- mongodb在ubutun上的安装
- MongoDB的安装和启动
- mongodb 通过mongodump来备份Sharded Cluste分片集群
- MongoDB安装
- mongodb 3 java教程
- 什么是MongoDB原子操作
- MongoDB学习整理
- mongodb工具类
- .NET Core也可以使用MongoDB了
- 使用MongoDB的开源项目(转)
- .NET Core也可以使用MongoDB了
- 在 CentOS7 上安装 MongoDB
- Mongoose轻松搞定MongoDB,不要回调!
- Mongoose轻松搞定MongoDB,不要回调!
- Mongodb学习-索引
- 如何在Django中使用mongodb数据库?
- Scala操作MongoDB
- NodeJS+Mongodb数据库实现简单在线应用程序