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

113
Visualizações
Check if a document's value is in an array MongoDB

I have ~ 200,000 documents in a collection which look like this:

{
 "_id": "tdhABqSZPEZ2fFcEzOVCb-q8d",
 "user": "testuser",
 "content": "Test Content"
}

And I have an array with ~50,000 entries:

let arr = ["tree", "apple", "test", "orange", ...otherEntries] // ~ 50,000 entries

I want to get all documents where any element of the array is in the content value, non case-sensitive, so that the example document above would be returned because in the array is test and in the content of the document is Test.

This would work using $where and then using a JavaScript expression but this is not very fast. Is there a really fast way (< 1-2 seconds) of doing a query like this or do you have any idea on how to restructure the documents that I can perform a fast query like this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • If you list(arr) was small => you could use make one index and use $in and filter (but its not small)

  • if you wanted case sensitive => you could make the list to a collection and $lookup with indexes (but you want case insensitive)

In you case that you have big list, and you want case insesitive

  • join the list into a big string(lets name it MyListString its a variable in your driver), separated with spaces for example ["hat" "tree"] to become "hat tree"

  • create a text index on content its very easy to do for example in Java i did mycoll.createIndex(Indexes.text("content")); see your driver documentation on how to create text indexes.

  • Do a find or an aggregation with match (MyListString is the above big string variable) (this does by default a case insensitive match)

    { "$match" { "$text" { "$search" MyListString} } }

Time was < 1 sec in my benchmark, test it, i think you will be fine.

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