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

128
Vistas
Project object from Nested Array MongoDB
[
  {
    "_id": "grandParentId",
    "types": [
      {
        "_id": "parentId",
        "files": [
          {
            "url": "1.example.com",
            "_id": "1childId"
          },
          {
            "url": "2.example.com",
            "_id": "2childId"
          }
        ]
      }
    ]
  }
]

cond: { _id: 2childId }

expected output:

{ 
    "url": "2.example.com",
    "_id": "2childId"
}

question 2: is this a good approach or should I just use loops to get the desired output?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

useing aggregate $unwind and $project

test it at mongoPlayground

db.collection.aggregate([
  {
    "$unwind": "$types"
  },
  {
    "$unwind": "$types.files"
  },
  {
    "$match": {
      "types.files._id": "2childId"
    }
  },
  {
    "$project": {
      "url": "$types.files.url",
      "_id": "$types.files._id"
    }
  }
])
about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm sure that there are other ways, but you can use mongo aggregation framework:

db.collection.aggregate([
  {
    $match: {
      "types.files._id": "2childId"
    }
  },
  {
    $unwind: "$types"
  },
  {
    $unwind: "$types.files"
  },
  {
    $replaceRoot: {
      newRoot: "$types.files"
    }
  },
  {
    $match: {
      _id: "2childId"
    }
  }
])

The first $match is just to filter documents which match with your condition. $unwind are used two times to deconstruct the different arrays. $replaceRoot replaces the input document with the specified document (in this case subdocument, files) and final $match filters files arrays that we deconstructed previously, based on your condition. First $match is just for performance reasons, you can delete it if you want.

You can see how stages works in https://mongoplayground.net/p/hoz24xP_BNV

See mongo aggregation framework documentation if you want to learn more about it (https://docs.mongodb.com/manual/aggregation/)

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