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

354
Views
How can you get more than one $or condition into a single mongodb find() operation?

I've got an operation where I'm trying to limit results on a collection using the $or operator, however I have more than one field that needs to utilize this functionality.

See query below:

db.items.find({//"isDeleted":false,
    "$or":[
        {"$and":[{"passages.fleschKincaid.readingEase":{"$gte":90}}]},
        {"$and":[{"passages.fleschKincaid.readingEase":{"$gte":60}},{"passages.fleschKincaid.readingEase":{"$lte":70}}]},
        {"$and":[{"passages.fleschKincaid.readingEase":{"$gte":50}},{"passages.fleschKincaid.readingEase":{"$lte":60}}]}
    ],
    "$or":[{"$and": [{"contentBanks":{$in: element.statusIds}}]}]})

The first condition loops through and adds on gte/lte operators based on certain conditions, and the second or condition I've used here is within a for loop that pushes on an additional $and for each new contentBank. Each one only needs to satisfy one of the conditions to show as a result, but contentBank and passages.fleschKincaid.readingEase are mutually exclusive so they can't be grouped together in the same $or group.

i.e. - if I have an item, for it to meet my conditions in the above example, it needs both a readingEase score of let's say 55, AND have a contentBank in statusIds. It needs to meet both conditions, but check the $or's separately.

What I end up with is a query that overwrites the written conditions and treats it as one big $or, returning results that might match one contentBank but not be within the readingEase range.

Any help would be greatly appreciated. I'm writing this with javascript, mongoose, and mongodb. If you know of a way to get the query to treat them separately that would be great.

p.s. - I'm trying to avoid it getting to chock-full of $and operators, but if that's the only way to do this, I can find a way to create a whole new object and add it in. The only issue with that is the complexity increases quite a bit because the pipeline operators are all built dynamically - so the additional $and operator wrapping everything would require a solid amount of changes/overhead to get working.

about 4 years ago · Juan Pablo Isaza
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!