mongodb 分组求和 问题

蜡笔小xin 发布于 2017/10/21 10:12
阅读 105
收藏 0

mongodb中user集合中存在如下数据:

{uid,"1","vn":"LB23","time":"2017-10-01",“record”:123}

{uid,"1","vn":"LB24","time":"2017-10-03",“record”:133}

{uid,"1","vn":"LB24","time":"2017-10-02",“record”:123}

{uid,"1","vn":"LB24","time":"2017-10-04",“record”:1222}

{uid,"1","vn":"LB25","time":"2017-10-01",“record”:123}

{uid,"1","vn":"LB25","time":"2017-10-02",“record”:1223}

结果是根据vn统计出最大时间数据然后在统计record的总和,但是自己在查询的时候先分组求出最大时间条数,然后在根据子文档进行求总数

 { "$group" : { "_id" : { "vn" : "$vn" } , "collect_time" : { "$max" : "$time"}}} 通过这个命令后得到的数据集合是:

{“_id”:{"vn":"LB25"},"time":"2017-10-02"}

{“_id”:{"vn":"LB24"},"time":"2017-10-04"}

...

此处没有包含record数据 

请问一下 我该如何修改呢

加载中
0
mickelfeng
mickelfeng

欢迎入坑

蜡笔小xin
蜡笔小xin
此坑可否踩过。。。
返回顶部
顶部