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

169
Vistas
How to project after lookup without the 'as' field mongoose

I have the following code:

     const products: ReadonlyArray<Pick<IProduct, 'id' | 'gender' | 'category' | 'title' | 'description' | 'price' | 'imageFileName'>> = await UserDB.aggregate<
        Pick<IProduct, 'id' | 'gender' | 'category' | 'title' | 'description' | 'price' | 'imageFileName'>>([
          {
            $match: { _id: mongoose.Types.ObjectId(req.userId) },
          },
          {
            $lookup: {
              from: 'products',
              localField: 'inBagProducts',
              foreignField: '_id',
              as: 'product',
            },
          },
          {
            $unwind: '$product',
          },
          {
            $project: { 
              product: {
                'product.category': 1,
                'product.gender': 1,
                'product.title': 1,
                'product.description': 1,
                'product.price': 1,
                'product.imageFileName': 1,
              }
            },
          },
        ]);

This code gives this as a result:

    {
        _id: 611e2febb863ce74ac448220,
        product: {
          category: 1,
          gender: 2,
          title: 'jdwoeid',
          description: 'jfwoeifjweoi',
          price: 23902,
          imageFileName: '1628808613195-7886.png'
        }
      },
      {
        _id: 611e2febb863ce74ac448220,
        product: {
          category: 5,
          gender: 1,
          title: 'sivdosi',
          description: 'oisbdvoi',
          price: 2394,
          imageFileName: 'http://localhost:3000/images/1628875244435-3564.png'
        }
      }

Expected result is this:

      {
        _id: 611e2febb863ce74ac448220,
          category: 1,
          gender: 2,
          title: 'jdwoeid',
          description: 'jfwoeifjweoi',
          price: 23902,
          imageFileName: '1628808613195-7886.png'
      },
      {
        _id: 611e2febb863ce74ac448220,
          category: 5,
          gender: 1,
          title: 'sivdosi',
          description: 'oisbdvoi',
          price: 2394,
          imageFileName: 'http://localhost:3000/images/1628875244435-3564.png'
      }

How can I have the result without the product: {}

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

0

You can use $addFields pipeline to add _id to the product property, and then you can use $replaceRoot pipeline. You can add these aggregation stages to your code:

[
  {
    "$addFields": {
      "product._id": "$_id"
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$product"
    }
  }
]

Here is the working example: https://mongoplayground.net/p/VVxXgDYFANI

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