mongodb的聚合函数的$skip + $limit 方法运用 和 顺序优化。

来源:互联网 发布:linux开发板推荐 编辑:程序博客网 时间:2024/05/17 07:22

转自: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
        }
    ]
}

1 0
原创粉丝点击