mongodb - 如何在 find 方法中使用计算字段和条件进行查询?
问题描述
我真的不知道如何做到这一点。基本上在我的文档中我有一个费率,我想找到计算出的费率低于某个值的文档。计算的费率如下:
if (rate < 20) {
calculated_rate = rate * 1.25;
} else {
calculated_rate = rate + 5;
}
解决方案
aggregate(
[
{
"$addFields" : {
"calculated_rate" : {
"$cond" : [
{
"$lt" : [
"$AreasServedRateAndConditions.CGHourlyRate",
20.0
]
},
{
"$multiply" : [
"$AreasServedRateAndConditions.CGHourlyRate",
1.25
]
},
{
"$add" : [
"$AreasServedRateAndConditions.CGHourlyRate",
5.0
]
}
]
}
}
},
{
"$match" : {
"$and" : [
{
"calculated_rate" : {
"$lte" : 25.0
}
}
]
}
}
],
{
"allowDiskUse" : false
}
);
推荐阅读
- java - 在 Java 中将字符串转换为日期时出错
- c# - 在 C# 中使用花括号释放内存
- python - Python - 元类装饰器 - 如何使用 @classmethod
- git - 使用 git init 命令时在 Gitbash 中出现“权限被拒绝”错误
- scikit-learn - sklearn Dictionarylearning - 如何验证字典?
- php - 基于用户输入的实时 MySQL 数据库搜索
- python - Python正则表达式确定新行并排除\ W
- powershell - 计算导入的 Excel 工作簿的列数 - 计算给定对象的属性
- node.js - 可以让异步函数处理请求吗?
- android - Android:如何以编程方式设置 LinearLayout 边距