本文介绍了Mongo DB聚合多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想投影一个仅当字段在范围内时才应用导出值的集合.
I want to project a collection applying exporting a value only if a field is inside a range.
排序:
db.workouts.aggregate({ $match: { user_id: ObjectId(".....") } }, { $project: { '20': { $cond: [ {$gt: [ "$avg_intensity", 20]} , '$total_volume', 0] } } })仅当avg_intensity在一定范围内时,我才需要获取该值.然后,我将对投影结果进行分组和求和.
I need to get the value only if the avg_intensity is inside a certain range. I will then group and sum on the projection result.
我想做的是应用$ gt和$ lt过滤器,但没有成功.
What I am trying to do is applying a $gt and $lt filter but with no much success.
db.workouts.aggregate( { $match: { user_id: ObjectId("....") } }, { $project: { '20': { $cond: [ [{$gt: [ "$avg_intensity", 20]}, {$lt: [ "$avg_intensity", 25]}] , '$total_volume', 0] } } })如何同时应用$ gt和$ lt条件?
How may I apply both $gt and $lt conditions?
推荐答案在下合并逻辑条件 $cond 运算符,然后使用/包装条件> $and 运算符:
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 ]} }} ])更多推荐
Mongo DB聚合多个条件
发布评论