Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
Mongodb Compare two fields with ratio

Rows whose field is twice that of the other field explain with example :

{id : 1 , price : 10 , spent : 5}
{id : 1 , price : 20 , spent : 30}
{id : 1 , price : 40 , spent : 90}
{id : 1 , price : 80 , spent : 200}

I want row that spend is equal or bigger twice price (spent >= 2*price)

result :

{id : 1 , price : 40 , spent : 90} ==> (spent >= 2price) [90>80]*

{id : 1 , price : 80 , spent : 200} ==> (spent >= 2price) [160>200]*

please refer suggestion except use $where

my mongo cluster have limitations of use $where

(MongoError: $where is not allowed in this atlas tier)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

MongoDB documentation is very good with examples.
There is a place Reference that has all the operators.
If you haven't found it, maybe try the bellow, i think this is what you need.

Query

  • we can refer to the field with $fieldName
  • and gt/multiply operators are used
  • finally $expr is used in match when we use aggregate operators (instead of query operaros) like we did here

Playmongo

aggregate(
[{"$match": {"$expr": {"$gt": ["$spent", {"$multiply": [2, "$price"]}]}}}])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!