1

来自 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
4

2 回答 2

1

好的,我想我得到了答案,通过使用array_agg

select city, array_agg(main.current_temp) as temp
from weather103
group by city
order by temp desc
于 2016-11-29T09:34:40.480 回答
0

@Hokutosei,欢迎来到 Couchbase!

查看这些资源和教程,让您快速开始查询。 http://query.pub.couchbase.com/tutorial/#1 http://developer.couchbase.com/documentation/server/4.5/getting-started/first-n1ql-query.html

我们也有非常活跃的论坛来提问。 https://forums.couchbase.com/

于 2016-11-29T17:57:13.753 回答