Mongo 结合排序和分组的数据库查询
Mongo DB query for combining sorting and grouping
假设我有一个包含以下(虚拟)数据的集合:
Country
State
Population Density (people per km^2)
Cases (in millions)
USA
New York
161
1.03
USA
California
95
4.47
Germany
Berlin
4,227
0.19
India
Kerala
859
4.09
India
Karnataka
319
2.95
India
Maharashtra
370
6.47
什么是优化的 mongodb 查询以获取按国家/地区分组并按州计数排序的结果,而且每个组应包含按 'Cases' 排序的州?
JSON
中的结果应如下所示
{
'results': [
{
'country' : 'India',
'num_states': 3,
'states': [
{
'State': 'Maharashtra',
'Cases': 6.47,
'PPD' : 370,
},
{
'State': 'Kerala',
'Cases': 4.09,
'PPD' : 859,
},
{
'State': 'Karnataka',
'Cases': 2.95,
'PPD' : 319,
}
]
},
{
'country' : 'USA',
'num_states': 2,
'states': [
{
'State': 'California',
'Cases': 4.47,
'PPD' : 95,
},
{
'State': 'New York',
'Cases': 1.03,
'PPD' : 161,
}
]
},
{
'country' : 'Germany',
'num_states': 1,
'states': [
{
'State': 'Berlin',
'Cases': 0.19,
'PPD' : 4227,
}
]
},
]
}
注:我的实际数据不一样,但是用例是一样的
第一 sort
个案例 -1
然后 group
按国家
添加新字段进行排序
和项目
db.collection.aggregate(
[
{
'$sort': {
'Cases': -1
}
}, {
'$group': {
'_id': {
'country': '$country'
},
'num_states': {
'$sum': 1
},
'states': {
'$push': {
'states': '$$ROOT.state',
'cases': '$$ROOT.Cases',
'ppd': '$$ROOT.Density'
}
}
}
}, {
'$addFields': {
'sorf': {
'$max': '$states.cases'
}
}
}, {
'$sort': {
'sorf': -1
}
}, {
'$project': {
'country': '$_id.country',
'num_states': '$num_states',
'states': 1,
'_id': 0
}
}
])
$sort
- 按案例降序排列
$group
- 按国家分组并构建一个州数组,并获得州总数
$sort
- 按州数降序排列
$project
- 重命名必填字段
db.collection.aggregate([
{
"$sort": {
"cases": -1
}
},
{
$group: {
_id: "$country",
num_states: {
$sum: 1
},
states: {
$push: {
state: "$state",
cases: "$cases",
PPD: "$population"
}
}
}
},
{
"$sort": {
"num_states": -1
}
},
{
$project: {
country: "$_id",
num_states: 1,
states: 1,
_id: 0
}
}
])
假设我有一个包含以下(虚拟)数据的集合:
Country | State | Population Density (people per km^2) | Cases (in millions) |
---|---|---|---|
USA | New York | 161 | 1.03 |
USA | California | 95 | 4.47 |
Germany | Berlin | 4,227 | 0.19 |
India | Kerala | 859 | 4.09 |
India | Karnataka | 319 | 2.95 |
India | Maharashtra | 370 | 6.47 |
什么是优化的 mongodb 查询以获取按国家/地区分组并按州计数排序的结果,而且每个组应包含按 'Cases' 排序的州?
JSON
中的结果应如下所示{
'results': [
{
'country' : 'India',
'num_states': 3,
'states': [
{
'State': 'Maharashtra',
'Cases': 6.47,
'PPD' : 370,
},
{
'State': 'Kerala',
'Cases': 4.09,
'PPD' : 859,
},
{
'State': 'Karnataka',
'Cases': 2.95,
'PPD' : 319,
}
]
},
{
'country' : 'USA',
'num_states': 2,
'states': [
{
'State': 'California',
'Cases': 4.47,
'PPD' : 95,
},
{
'State': 'New York',
'Cases': 1.03,
'PPD' : 161,
}
]
},
{
'country' : 'Germany',
'num_states': 1,
'states': [
{
'State': 'Berlin',
'Cases': 0.19,
'PPD' : 4227,
}
]
},
]
}
注:我的实际数据不一样,但是用例是一样的
第一 sort
个案例 -1
然后 group
按国家
添加新字段进行排序
和项目
db.collection.aggregate(
[
{
'$sort': {
'Cases': -1
}
}, {
'$group': {
'_id': {
'country': '$country'
},
'num_states': {
'$sum': 1
},
'states': {
'$push': {
'states': '$$ROOT.state',
'cases': '$$ROOT.Cases',
'ppd': '$$ROOT.Density'
}
}
}
}, {
'$addFields': {
'sorf': {
'$max': '$states.cases'
}
}
}, {
'$sort': {
'sorf': -1
}
}, {
'$project': {
'country': '$_id.country',
'num_states': '$num_states',
'states': 1,
'_id': 0
}
}
])
$sort
- 按案例降序排列$group
- 按国家分组并构建一个州数组,并获得州总数$sort
- 按州数降序排列$project
- 重命名必填字段
db.collection.aggregate([
{
"$sort": {
"cases": -1
}
},
{
$group: {
_id: "$country",
num_states: {
$sum: 1
},
states: {
$push: {
state: "$state",
cases: "$cases",
PPD: "$population"
}
}
}
},
{
"$sort": {
"num_states": -1
}
},
{
$project: {
country: "$_id",
num_states: 1,
states: 1,
_id: 0
}
}
])