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

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

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 Denunciar

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 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