轉自:http://docs.mongoing.com/manual-zh/core/aggregation-pipeline-optimization.html
$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
}
]
}