Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
规范化来自MongoDB的结果_Mongodb_Aggregation Framework - Fatal编程技术网

规范化来自MongoDB的结果

规范化来自MongoDB的结果,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我需要在MongoDB表上执行以下操作。我需要根据一些列值筛选文档,我需要按列分组,并根据聚合查找值的计数。此外,我希望规范化计数值(即,将结果的计数值除以该结果的最大计数值) 我使用聚合管道的$match和$group参数完成了前两个步骤。我不确定如何对结果部分进行规范化 我当前的查询如下所示 db.list_input_file.aggregate([ {$match:{ 'content.Year' : {$eq : '2006'} }}, {$group:{'_id':'$con

我需要在MongoDB表上执行以下操作。我需要根据一些列值筛选文档,我需要按列分组,并根据聚合查找值的计数。此外,我希望规范化计数值(即,将结果的计数值除以该结果的最大计数值)

我使用聚合管道的$match$group参数完成了前两个步骤。我不确定如何对结果部分进行规范化

我当前的查询如下所示

db.list_input_file.aggregate([ 
 {$match:{ 'content.Year' : {$eq : '2006'} }}, 
 {$group:{'_id':'$content.Author', count:{$sum:1}}} 
])
我认为这可以通过(代码之间的解释)完成


哇!谢谢你。我自己永远也搞不清楚这件事!
db.list_input_file.aggregate([ {
    $match : {
        'content.Year' : {
            $eq : '2006'
        }
    }
}, {
    $group : {
        '_id' : '$content.Author',
        count : {
            $sum : 1
        }
    }
}, {
    $group : {
        _id : 0,
        maxCount : {            // get the largest count value
            $max : "$count"
        },
        docs : {                // push all documents into one field to store as an array
            $push : "$$ROOT"
        }
    }
}, {
    $project : {
        _id : 0,
        docs : {
            $map : {
                "input" : "$docs",
                "as" : "e",
                "in" : {                    // retrieve each element
                    _id : "$$e._id",
                    count : "$$e.count",
                    rate : {                // add the normalized value here
                        $divide : [ "$$e.count", "$maxCount"]
                    }
                }
            }
        }
    }
}, {
    $unwind : "$docs"
}, {
    $project : {
        _id : "$docs._id",
        count : "$docs.count",
        rate : "$docs.rate"
    }
} ]);