Multiple group operations using Mongo aggregation framework

前端 未结 1 651
悲哀的现实
悲哀的现实 2020-12-24 14:26

Given a set of questions that have linked survey and category id:

> db.questions.find().toArray();
[
    {
        \"_id\" : ObjectId(\"4fda05bc322b1c95b5         


        
相关标签:
1条回答
  • 2020-12-24 14:38

    It's important to understand that the operations in the argument to aggregate() form a pipeline. This meant that the input to any element of the pipeline is the stream of documents produced by the previous element in the pipeline.

    In your example, your first query creates a pipeline of documents that look like this:

    {
        "_id" : 2,
        "avg_score" : 5.5
    },
    {
        "_id" : 1,
        "avg_score" : 4
    }
    

    This means that the second element of the pipline is seeing a series of documents where the only keys are "_id" and "avg_score". The keys "category_id" and "score" no longer exist in this document stream.

    If you want to further aggregate on this stream, you'll have to aggregate using the keys that are seen at this stage in the pipeline. Since you want to average the averages, you need to put in a single constant value for the _id field, so that all of the input documents get grouped into a single result.

    The following code produces the correct result:

    db.questions.aggregate(
        { $group : {
            _id : "$category_id",
            avg_score : { $avg : "$score" },
            }
        },
        { $group : {
            _id : "all",
            avg_score : { $avg : "$avg_score" },
            }
        }
    );
    

    When run, it produces the following output:

     {
        "result" : [
            {
            "_id" : "all",
            "avg_score" : 4.75
            }
        ],
        "ok" : 1
     }
    
    0 讨论(0)
提交回复
热议问题