mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Feature: Flatten nested populate

Open khaledosama999 opened this issue 4 years ago • 4 comments

Feature

As of right now we can have nested populate like so :

const street = await StreetModel
     .findOne(
       { _id: districtId}
      )
     .populate({
       path: 'district',
       model: 'District',
       populate: {
         path: 'city',
         model: 'City'
       },
     })
     .lean();

This means in order to access city you will have to do the following street.district.city

Wanted behavior

I think we should allow an optional way to flatten the nested population in order to allow the response to have a better structure if desired, instead of having to access nested properties so in the end we can access district like so street.district and city like so street.city. I can make a PR but from/to which branch should it be ?

Use case

In a single nested level, this won't make much of a difference but for me, I had these kind of relations Region->city->district->sub-district->area->street :D, sometimes I need the street with all of the locations above it

khaledosama999 avatar Sep 02 '21 13:09 khaledosama999

@IslandRhythms Any updates ?? I can start a PR but on which branch ??

khaledosama999 avatar Sep 03 '21 11:09 khaledosama999

You can make a PR off master

IslandRhythms avatar Sep 03 '21 17:09 IslandRhythms

any update @khaledosama999 ? it would be a nice feature!

lucafaggianelli avatar Dec 14 '21 13:12 lucafaggianelli

@lucafaggianelli got hung up on work, working on it right now

khaledosama999 avatar Jan 01 '22 16:01 khaledosama999