rice_z
rice_z
  • 发布:2023-09-22 10:57
  • 更新:2023-09-22 11:05
  • 阅读:210

groupField 里面使用 sum 能只求满足条件的和吗

分类:uniCloud

比如数据时这样的

{    
  _id: "1",    
 date:'2023-09-01',  
  type: 0,  
  count:10  
}{    
  _id: "1",    
 date:'2023-09-01',  
  type: 1,  
  count:20  
},  
{    
  _id: "1",    
 date:'2023-09-06',  
  type: 0,  
  count:30  
}

这是查询语句
db.collection('xxx').groupBy('date').groupField('sum(xxx) as sub,sum(xxxx) as add')

我想要返回的结果是

[{  
  date:'2023-09-01',  
  sub:10,  
  add:20  
},{  
  date:'2023-09-06',  
  sub:30,  
  add:0  
}]

请教一下 sum里面该怎么写表达式呢 ,我这样写是无效的 ('sum(type==0?count:0) as sub,sum(type==1?count:0) as add')

2023-09-22 10:57 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

groupBy前把type 0的过滤掉不行吗

  • rice_z (作者)

    不行吧,我这里要统计的是每日的数据中, type=0 和type=1 count的总和分别是多少,如果要在groupBy前把type 0的过滤掉,那不是要查两次数据库了,第二次就要过滤掉type=1 的数据了

    2023-09-22 11:25

  • DCloud_uniCloud_WYQ

    回复 1***@qq.com: 那就把type也加到groupBy里面

    2023-09-22 11:45

要回复问题请先登录注册