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

Viewed 50

I am fairly new to MongoDB and I am trying to create a filter that combines more than 1 property.

For example, let us say that I want to filter the search results based on whether the multiplication of two properties a and b from model Object is more than threshold.

Would I do something like this?

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

I have tried this, but I am getting all sorts of errors.

1 Answers

You can use the expression operator $expr to put condition for internal fields,

  • do multiplication by $multiply operator
  • $gt operator to match with threshold field
Object.find({
  $expr: {
    $gt: [
      { $multiply: ["$a", "$b"] },
      "$threshold"
    ]
  }
})

Playground

Related