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

205
Vistas
How to get expected output from MongoDB?

I'm new to MongoDB aggregation. I am not getting desired output The output I'm getting from aggregation:-

[
{tweet:{key:value}},
{tweet:{key:value}},
{tweet:{key:value}},
{tweet:{key:value}},
]

but I want the following output from the pipeline:-

[
{key:value},
{key:value},
{key:value},
]

and lastly, pipeline I'm running:-

const pipeline = [[
            {
              $match: {
                $expr: {
                  $in: [
                    Mongoose.Types.ObjectId(userid), '$likedBy.user'
                  ]
                }
              }
            }, {
              $lookup: {
                from: 'tweets', 
                localField: 'tweet', 
                foreignField: '_id', 
                as: 'tweet'
              }
            }, {
              $unwind: {
                path: '$tweet'
              }
            }, {
              $lookup: {
                from: 'users', 
                localField: 'tweet.user', 
                foreignField: '_id', 
                as: 'user'
              }
            }, {
              $unwind: {
                path: '$user'
              }
            }, {
              $addFields: {
                'tweet.user': '$user'
              }
            },
            {
                $addFields: {
                  'tweet.isLiked': true,
                }
              },{
                  $project:{
                      tweet:1,
                  }
              },
          ]
        ];
        const likedTweets = await TweetLike.aggregate(pipeline)

I know I can do this with javascript but I want to do it with the pipeline

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

0

You can replace your last project stage with the following to achieve what you need:

{$project:{key:"$tweet.key"}}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Answering my own question

i wanted to return sub-document so i found this https://stackoverflow.com/a/43411988/12332711

all i had to do is use

{
 $replaceRoot: {newRoot: "$tweet"}
 }

it worked for me

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