Automattic / Automattic/mongoose

Feature: Flatten nested populate

Open
#10,661 4 comments 0 reactions 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

### Feature

As of right now we can have nested populate like so :
```js
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

Contributor guide

Open the contributing guide

Research direction

No files, tests, or entry points are identified. Start by tracing the existing nested populate behavior and defining how an optional flattening form should represent district and city; done means nested population can opt into top-level fields without breaking the current nested structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.