Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
¿Cómo obtener el resultado esperado de MongoDB?

Soy nuevo en la agregación de MongoDB. No obtengo el resultado deseado El resultado que obtengo de la agregación: -

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

pero quiero el siguiente resultado de la canalización: -

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

y, por último, canalización que estoy ejecutando: -

 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)

Sé que puedo hacer esto con javascript pero quiero hacerlo con la canalización

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede reemplazar su última etapa del proyecto con lo siguiente para lograr lo que necesita:

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

0

Respondiendo mi propia pregunta

quería devolver el subdocumento, así que encontré esto https://stackoverflow.com/a/43411988/12332711

todo lo que tenía que hacer era usar

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

funcionó para mí

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!