user12682244
user12682244

Reputation: 21

MongoDb Query Filtering with arithmetic expression

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

Upvotes: 0

Views: 165

Answers (1)

nimrod serok
nimrod serok

Reputation: 16033

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

Upvotes: 1

Related Questions