因此,我想要做的是获得每个省、每个类型提供的体育活动的总数.柏林和科隆的活动项目是 run 和游泳 输出应如下所示:
{
"_id" : {
"province" : "Berlin",
"type" : "Running"
},
"number" : 2.0
}
{
"_id" : {
"province" : "Berlin",
"type" : "Swimming"
},
" number" : 2.0
}
{
"_id" : {
"province" : "Cologne",
"type" : "Running"
},
"number" : 2.0
}
{
"_id" : {
"province" : "Cologne",
"type" : "Swimming"
},
"number" : 1.0
}
我的查询如下所示,我没有得到我想要的结果.它向我展示了哪些省份的 run 和游泳是不是柏林或科隆的活动.我试着输入$或插入$,但仍然没有产生我想要的结果.我遗漏了什么?
db.getCollection("Sport").aggregate([
{$match: {$or: [{province: "Cologne"}, {province: "Berlin"}], $or: [{type: "Running"}, {type: "Swimming"}]}},
{$group: {_id: {province: "$province", type: "$type"}, count: {$sum: 1}}},
{$sort: {count: -1}}
])