2016-11-29 24 views
1

来自mongoDB 5年,现在只是研究/尝试Couchbase。我有一个测试数据,我想按城市分组,并将所有main.temp值收集/推送到一个字段。这可以在MongoDB中聚集由$push$addToSetCouchbase集团的名称和收集字段值

测试数据来完成

{ 
    "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 

回答

1

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

select city, array_agg(main.current_temp) as temp 
from weather103 
group by city 
order by temp desc 
+0

看起来正确, 为了清楚起见,请附上您的结果输出。 –