mongodb - Mongo db aggregation multiple conditions -
i want project collection applying exporting value if field inside range.
sort of:
db.workouts.aggregate({  $match: { user_id: objectid(".....") } }, {  $project: {          '20': { $cond: [ {$gt: [ "$avg_intensity", 20]} , '$total_volume', 0] }     }      }) i need value if avg_intensity inside range. group , sum on projection result.
what trying applying $gt , $lt filter no success.
db.workouts.aggregate( {    $match: { user_id: objectid("....") } }, { $project: {          '20': { $cond: [ [{$gt: [ "$avg_intensity", 20]}, {$lt: [ "$avg_intensity", 25]}] ,    '$total_volume', 0] }     }      }) how may apply both $gt , $lt conditions?
to  combine logical conditions under $cond operator wrap conditions $and operator:
db.workouts.aggregate([     { "$match": { "user_id": objectid("....") }},     { "$project": {         "20": { "$cond": [            { "$and": [                 { "$gt": [ "$avg_intensity", 20 ] },                { "$lt": [ "$avg_intensity", 25 ] }            ]},                "$total_volume",             0        ]}    }} ]) 
Comments
Post a Comment