我必须计算no和no之间的比率。按每个行政区的菜肴分类的餐厅数量。为此,我按照行政区进行分组,并以分母为单位获取餐馆的数量,并通过匹配和分组的方式来获取关于菜肴的行政区数量。
使用查询: dict2= []
a={"$facet":{T:[{ "$group" : { "_id" : "$borough","total" : { "$sum" :1 } } }],TT:[{ "$match" : { "cuisine" : "American " } },{"$bucket":{ "$group" : { "_id" : "$borough","total" : { "$sum" :1 } } }}]}
dict2.append(a)这是在python中运行的,使用管道technique.This会导致语法错误。这里可能缺少and (A).What。
发布于 2020-02-29 09:20:01
我不知道totalC是怎么进来的
command3 = { "$group" : { "_id" : "$borough", "totalC" : { "$sum" :1 } } }与total在以下方面不同
command0 = { "$group" : { "_id" : "$borough","total" : { "$sum" :1 } } }我想你要找的是最重要的:-总餐馆-按菜系分类的餐馆-按区分类的餐馆-按区和菜系分类的餐馆
然后用这些数字进行一些算术运算。
$facet阶段可能会让您开始:
db.collection.aggregate([
{$facet:{
borough:[
{$group:{_id:{borough:"$borough",cuisine:"$cuisine"}, count:{$sum:1}}},
{$group:{_id:"$_id.borough",cuisine:{$push:{cuisine:"$_id.cuisine",count:"$count"}}, count:{$sum:"$count"}}}
]
cuisine:[{$group:{_id:"$cuisine",count:{$sum:1}}},
total:[{$count:"$count"}]
}},
])这将为您提供一个包含3个数组的文档,类似于:
{
borough:[{_id:"boroughname", count:xx, cuisine:[{_id:"cuisinename", count:xx},...]},...]
cuisine:[{_id:"cuisinename", count:xx},...],
total:[{count:xxx}]
}https://stackoverflow.com/questions/60460864
复制相似问题