MongoDB's aggregation functions are $skip + $limit methods applied and sequential optimized.

Source: Internet
Author: User
Tags mongodb

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
}
]
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.