Mongo db aggregation multiple conditions

点点圈 提交于 2019-12-05 00:23:48

To combine logical conditions under a $cond operator then wrap the conditions with an $and operator:

db.workouts.aggregate([
    { "$match": { "user_id": ObjectId("....") }},
    { "$project": { 
       "20": { "$cond": [
           { "$and": [ 
               { "$gt": [ "$avg_intensity", 20 ] },
               { "$lt": [ "$avg_intensity", 25 ] }
           ]},    
           "$total_volume", 
           0
       ]}
   }}
])

If I got your requirements right you should put the filter in the match part of the pipeline:

db.workouts.aggregate( [
  { $match: { user_id: ObjectId("...."), "avg_intensity": { $gt: 20, $lte: 25 } } },
  { $group: { _id: ..., count: ... } }
] );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!