有什么方法可以在 MongoDB 中创建一个具有多个值的过滤器吗?

Is there some way to create a filter with more than one value in MongoDB?

我是 MongoDB 的新手,我正在尝试创建一个组合超过 1 个 属性 的过滤器。

例如,假设我想根据模型 Object 的两个属性 ab 的乘积是否大于 threshold.

我会做这样的事情吗?

Object.find({
   a * b : {$gt: threshold}
});

我试过了,但是我遇到了各种各样的错误。

您可以使用表达式运算符 $expr 为内部字段设置条件,

  • $multiply运算符做乘法
  • $gt 运算符与 threshold 字段匹配
Object.find({
  $expr: {
    $gt: [
      { $multiply: ["$a", "$b"] },
      "$threshold"
    ]
  }
})

Playground