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

144
Views
How to create indexes in Mongo when having a query with AND's and OR's together?

I've read that for only OR queries like:

find($or [{field1:a}, {field2:b}])

an index for each field shall be created (simple or compound with the fields as prefix). For AND queries like:

find(field1:a, field2:b)

a compound index shall be created to cover it throughly. But what to do when a query has AND's and OR's together?

find($or[field1:a, field2:b], field3:c, field4:d)

What are all of the indexes to be created in this case?

MongoDB 4.4.6

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

TL;DR: You probably do not need a compound index here but rather individual field indexes on the highest cardinality, most performance-demanding queries.

Compound indexes are very useful when your AND pattern is predictably hierarchical. If a compound index is created on fields A,B,C then a query on A will be fast, A,B will be fast, and A,B,C will be (really) fast. Query on just B cannot use the index, nor B,C, nor C.

If you run your query with explain(true) and examine the output, you will see that multiple indexes can be used for both $or and $and types of queries.

over 4 years ago · Santiago Trujillo 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!