mongodb how to use aggregate like populate mongodb how to use aggregate like populate mongoose mongoose

mongodb how to use aggregate like populate


To make it easier i would not change the current pipeline but just add a $group stage to end of it in order to re-structure the data.

{    $group: {        _id: "$_id",        user: {$first: "$user"},        dream: {$addToSet: "$dream"},        works: {$addToSet: "$works"}    }}

With that said if you are using Mongo version 3.6+ i do recommend you use the "newer" version of $lookup to re-write your pipeline to be a bit more efficient by avoiding all these $unwind's.