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
MongoDB find join array

I have a collection with data that looks sort of like this

{
  "part": [
    { "a": "1", "b": "a" },
    { "a": "23", "b": "b" },
    { "a": "4", "b": "c" },
  ]
}

What I would like is a way of searching for documents where the join of all a parts equals the search that I am looking for.

for example 1234 should match the document above, but 124 should not.

is this possible with MongoDB?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can do it with Aggregation framework:

  • $match with $eq - To filter only documents where concatenated a properties of part array are equal to the input string.
  • $reduce with $concat - To concatenate all a properties of part array for each document.
db.collection.aggregate([
  {
    "$match": {
      "$expr": {
        "$eq": [
          "1234",
          {
            "$reduce": {
              "input": "$part",
              "initialValue": "",
              "in": {
                "$concat": [
                  "$$value",
                  "$$this.a"
                ]
              }
            }
          }
        ]
      }
    }
  }
])

Working example

over 4 years ago · Santiago Trujillo Relatório

0

You can use aggregate with $reduce to join string then $match to filter your string.
Here is the playground.

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