在 mongodb 中使用“group by”选择 Max()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10612660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select Max() with "group by" in mongodb
提问by spidermanit
Please help me to convert this select sentence to mongodb:
请帮我把这个select语句转换成mongodb:
Select Name, Max(Value) From table1 Group By Name
I read this document: http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group
我阅读了这份文件:http: //www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group
but still dont know how to apply Max() method instead SUM() as that document.
但仍然不知道如何应用 Max() 方法而不是 SUM() 作为该文档。
Thank you.
谢谢你。
回答by Parvin Gasimzade
I have created Mongo Collection as follows.
我按如下方式创建了 Mongo 集合。
{ "_id" : ObjectId("4fb36bfd3d1c88bfa15103b1"), "name" : "bob", "value" : 5 }
{ "_id" : ObjectId("4fb36c033d1c88bfa15103b2"), "name" : "bob", "value" : 3 }
{ "_id" : ObjectId("4fb36c063d1c88bfa15103b3"), "name" : "bob", "value" : 7 }
{ "_id" : ObjectId("4fb36c0c3d1c88bfa15103b4"), "name" : "john", "value" : 2 }
{ "_id" : ObjectId("4fb36c103d1c88bfa15103b5"), "name" : "john", "value" : 4 }
{ "_id" : ObjectId("4fb36c143d1c88bfa15103b6"), "name" : "john", "value" : 8 }
{ "_id" : ObjectId("4fb36c163d1c88bfa15103b7"), "name" : "john", "value" : 6 }
Then by using the following code I group it by their name and max(value)
然后通过使用以下代码,我按名称和 max(value) 对其进行分组
db.table1.group(
{key: {name:true},
reduce: function(obj,prev) {
if (prev.maxValue < obj.value) {
prev.maxValue = obj.value;
}
},
initial: { maxValue: 0 }}
);
The result is shown as
结果显示为
[
{
"name" : "bob",
"maxValue" : 7
},
{
"name" : "john",
"maxValue" : 8
}
]
It is much simpler with the aggregation framework. You can get the same result with the following code by using aggregation framework.
使用聚合框架要简单得多。您可以使用聚合框架通过以下代码获得相同的结果。
db.table1.aggregate(
{$group:{_id:"$name", "maxValue": {$max:"$value"}}}
);
回答by Smoggit
Using the Aggregation Framework:
使用聚合框架:
db.table1.aggregate({$group:{'_id':'$name', 'max':{$max:'$value'}}},
{$sort:{'max':1}}).result
回答by Kiran Chaudhari
var myresult = db.table1.aggregate([{
$group: {
_id:"$Name",
value: { $max: "$Value" }
}
}]);
print(myresult)
回答by Tejas
Since MongoDB supports mapreduce below function should do.
由于 MongoDB 支持 mapreduce 下面的函数应该做。
db.employee.insert({name:"Tejas",Value:2})
db.employee.insert({name:"Tejas",Value:3})
db.employee.insert({name:"Varma",Value:1})
db.employee.insert({name:"Varma",Value:6})
var map=function(){
var key={name:this.name};
var value={value:this.Value};
emit(key,value);
};
var reduce=function(key,values){
var max=-1;
values.forEach(function(value){
if(max==-1){
max=value['value'];
}
if(max<value['value']){
max=value['value'];
}
});
return {max:max};
};
db.employee.mapReduce(map,reduce,{out:{inline:1}});
db.employee.mapReduce(map,reduce,{out:{inline:1}});