Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

mongo的复杂聚合分组查询

[  
 {time:'3月',type:'aa'},      
 {time:'3月',type:'aa'},
 {time:'3月',type:'aa'},
 {time:'3月',type:'bbb'},
 {time:'4月',type:'aa'},  
 {time:'4月',type:'aa'},  
 {time:'5月',type:'aa'},  
 {time:'5月',type:'bbb'},   
]

数据库有以上的数据,
我想通过mongo的aggregate聚合分组,先按照时间维度分组,然后再在时间维度内按照type分组,最后得到如下结果:

[
  {time:'3月',type:{aa:3,bbb:1}},
  {time:'4月',type:{aa:2,bbb:0}},
  {time:'5月',type:{aa:1,bbb:1}},
]

请问mongo的二维分组聚合查询应该怎么写?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

看了一下文档,好像确实比较难,或者说不可以.
可以尝试一下$accumulator操作符, 文档地址: https://docs.mongodb.com/manual/reference/operator/aggregation/group/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...