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

356
Visualizações
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
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