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

199
Vistas
Need to aggregate and return results based on document nested array

I have the following document:

{
  "_id": ObjectId("10..."),
  "ownerName": "Merchant 10",
  ...,
  "stores": [
    {
      "id": ObjectId("20..."),
      "storeName": "Store 20"
    }, 
    {
      "id": ObjectId("21..."),
      "storeName": "Store 21"
    }
  ]
},
{
  "_id": ObjectId("11..."),
  "ownerName": "Merchant 11",
  ...,
  "stores": [
    {
      "id": ObjectId("22..."),
      "storeName": "Store 22"
    }
  ]
}

Is it possible with aggregate to return paginated results based on nested array "stores", and end up with the following result:

{
  "_id": "20...",
  "ownerName": "Merchant 10",
  "storeName": "Store 20"
}, {
  "_id": "21...",
  "ownerName": "Merchant 10",
  "storeName": "Store 21"
}, {
  "_id": "22...",
  "ownerName": "Merchant 11",
  "storeName": "Store 22"
}, 

So basically, I would like to be able to share owner info but returning result based on stores.

I tried another way with a stores document but with my specific requirement, I ended up with a circular dependency.

Thanks

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Using unwind on stores you can flatten out the documents. After that, just project the fields you need:

db.mycollection.aggregate( [
   { $unwind: "$stores" },
   { $project: { "ownerName": 1, "storeName": "$stores.storeName" } }
] )
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