Mongo DB聚合多个条件

编程入门 行业动态 更新时间:2024-10-26 00:18:46
本文介绍了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聚合多个条件

本文发布于:2023-11-22 18:46:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   条件   Mongo   DB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!