MongoDb Query Filtering with arithmetic expression

Viewed 19

i have a query in sql that I wan't to translate into the MongoDb query.

The statement is:

select * from TBA where a/b < c/d

a,b,c are columns in the table tba and d is a constant

How can I rewrite this statement into the MongoDb query language? I have a document collection called "TBA" where all documents are stored. Now I want to find out which documents fulfill the condition "a/b < c/d".

Thank you in advance.

Best regards, user12682244

1 Answers

If you want to do a calculation using the values stored in the document, you need to use a pipeline:

db.collection.aggregate([
  {$match: {
      $expr: {
        $lt: [
          {$divide: ["$a", "$b"]},
          {$divide: ["$c", d]}
        ]
      }
    }
  }
])

See how it works on the playground example

Related