Transferred from: http://docs.mongoing.com/manual-zh/core/aggregation-pipeline-optimization.html
$skip + $limit order optimization
If you have a pipeline with $skip followed by a $limit, the optimizer moves the $limit to the front of the $skip, at which point the $limit value is added to the number of $skip.
For example, if the pipeline consists of the following parts:
{$skip: ten},
{$limit: 5}
During the optimization phase, the optimizer transforms the sequence to the following:
{$limit:},
{$skip: 10}
For similar $sort + $limit merges, such as $sort + $skip + $limit, the optimizer allows you to do a lot of optimizations. See $sort + $limit merge for details, or see examples in $sort + $skip + $limit order.
For aggregations on a shard collection, the optimizer can reduce the number of documents returned from each shard.
1, Db.memos.find ({})
Query memos document results;
2, Db.memos.aggregate ({$skip: 3})
Skip the first three rows of query results;
3, Db.memos.aggregate ({$limit: 3})
Get the first three rows of query results;
1, Db.memos.find ({}) is as follows:
{
"_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
}
]
}