Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda