我正在使用餐厅样本数据集,其中的数据如下:
{
"_id" : ObjectId("5728c18870b1f4f542bd7c52"),
"borough" : "Brooklyn",
"cuisine" : "American",
"name" : "Riviera Caterer",
"restaurant_id" : "40356018"
}
{
"_id" : ObjectId("5728c18870b1f4f542bd7c55"),
"borough" : "Queens",
"cuisine" : "Jewish/Kosher",
"name" : "Tov Kosher Kitchen",
"restaurant_id" : "40356068"
}(跳过一些不需要的字段)
我想做的基本上是将数据按菜系分组,计数和菜式相邻,很像:
select count(1) as Count, cuisine
from restaurants
group by cuisine会屈服
Count | Cuisine
--------------
10 | American
5 | Indian试炼
db.restaurants.aggregate([
{"$group" : {_id:"$cuisine",
count_1:{$sum:1}}}
])结果如下:
{ "_id" : "American ", "count_1" : 6183 }
{ "_id" : "Bakery", "count_1" : 691 }
{ "_id" : "Irish", "count_1" : 190 }
{ "_id" : "Chicken", "count_1" : 410 }
{ "_id" : "Ice Cream, Gelato, Yogurt, Ices", "count_1" : 348 }
{ "_id" : "Egyptian", "count_1" : 14 }
{ "_id" : "Jewish/Kosher", "count_1" : 316 }
{ "_id" : "Delicatessen", "count_1" : 321 }
{ "_id" : "Indonesian", "count_1" : 8 }
{ "_id" : "Russian", "count_1" : 88 }
{ "_id" : "Spanish", "count_1" : 637 }
{ "_id" : "Chinese", "count_1" : 2418 }
{ "_id" : "Hamburgers", "count_1" : 433 }
{ "_id" : "Hotdogs", "count_1" : 34 }
{ "_id" : "Sandwiches/Salads/Mixed Buffet", "count_1" : 255 }
{ "_id" : "Pancakes/Waffles", "count_1" : 16 }
{ "_id" : "Turkish", "count_1" : 70 }
{ "_id" : "Caribbean", "count_1" : 657 }
{ "_id" : "Donuts", "count_1" : 479 }
{ "_id" : "Bagels/Pretzels", "count_1" : 168 }是否有可能得到这样的数据:
{ "American" : 6183 }
{ "Bakery" : 691 }
{ "Irish" : 190 }
{ "Chicken" : 410 }
{ "Ice Cream, Gelato, Yogurt, Ices" : 348 }
{ "Egyptian" : 14 }
{ "Jewish/Kosher" : 316 }
{ "Delicatessen": 321 }诸若此类?
尝试使用像这样的投影:
db.restaurants.aggregate(
{ $group: { "_id": {cuisine:"$cuisine"},
"Count": {"$sum":1}
}
},
{ "$project":
{ "_id": 0,
"Cuisine": "$_id.cuisine",
"Total":"$Count",
}
}
) 但并没有完全理解:
{ "Cuisine" : "American ", "Total" : 6183 }
{ "Cuisine" : "Bakery", "Total" : 691 }
{ "Cuisine" : "Irish", "Total" : 190 }
{ "Cuisine" : "Chicken", "Total" : 410 }
{ "Cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "Total" : 348 }
{ "Cuisine" : "Egyptian", "Total" : 14 }
{ "Cuisine" : "Jewish/Kosher", "Total" : 316 }
{ "Cuisine" : "Delicatessen", "Total" : 321 }
{ "Cuisine" : "Indonesian", "Total" : 8 }
{ "Cuisine" : "Russian", "Total" : 88 }
{ "Cuisine" : "Spanish", "Total" : 637 }
{ "Cuisine" : "Chinese", "Total" : 2418 }
{ "Cuisine" : "Hamburgers", "Total" : 433 }
{ "Cuisine" : "Hotdogs", "Total" : 34 }
{ "Cuisine" : "Sandwiches/Salads/Mixed Buffet", "Total" : 255 }
{ "Cuisine" : "Pancakes/Waffles", "Total" : 16 }
{ "Cuisine" : "Turkish", "Total" : 70 }
{ "Cuisine" : "Caribbean", "Total" : 657 }
{ "Cuisine" : "Donuts", "Total" : 479 }
{ "Cuisine" : "Bagels/Pretzels", "Total" : 168 }基本上,我希望下面的计数按字段分组,但在MongoDB中。
发布于 2016-05-11 21:55:43
尝尝这个
db.restaurants.aggregate([
{
"$group" : {_id:"$cuisine", count_1:{$sum:1}}
}
]).
result.
map(function(o) {
var r = {};
r[o._id] = o.count_1;
return r;
});发布于 2019-06-13 12:31:18
这将给出预期的输出。
db.restaurants.aggregate([
{
$group : {_id:"$cuisine", mycount:{$sum:1}}
}]).map(function(o) {
var r = {};
r[o._id] = o.mycount;
return r;
});https://stackoverflow.com/questions/37173402
复制相似问题