Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda