来自 mongoDB 5 年,现在只是学习/尝试 Couchbase。我有一个测试数据,我想按城市分组,并将所有main.temp
值收集/推送到一个字段。这可以通过$push
或在 mongoDB 聚合中完成$addToSet
测试数据
{
"city": "a",
"main": {
temp: 1
}
},
{
"city": "a",
"main": {
temp: 2
}
},
{
"city": "b",
"main": {
temp: 3
}
},
{
"city": "b",
"main": {
temp: 4
}
}
我希望结果是这样的,如果可能的话,我想将临时数组排序为 desc/asc
{
"city": "a",
"temp": [1, 2],
},
{
"city": "b",
"temp": [4, 3],
},
我使用管理员的 gui 查询尝试了一些东西,但它没有给我想要的确切结果。
select name, max(main.temp) as temp
from weather103
group by city