Automattic / Automattic/mongoose

Give the ability to have an array for foreignField

Open
#11,331 1 comment 0 reactions 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 5h
Merged PRs (30d)
32

Description

**Do you want to request a *feature* or report a *bug*?**
feature

**What is the current behavior?**
When you pass a String to localField and an array to foreignField property in Schema, the population will failed

**What is the expected behavior?**
I would like to be able to set an array of field name for foreignField when the localField is a String. Previously an issue has been created to request a feature to have an array for localField and foreignField, I know... But the meaning to have a String for localField and an array for foreignField isn't the same...
When we have a String for localField and an array for foreignField that would meaning that I want all document wich have, at least, one of the property in the foreignField array equals to the localField value.

Currently, to support this feature only two lines ( one is the typing for typescript ) has to be changed :
- https://github.com/Automattic/mongoose/blob/master/lib/helpers/populate/getModelsMapForPopulate.js#L548
We just have to replace this line by :
```
foreignField: new Set(Array.isArray(data.foreignField) ? data.foreignField : [data.foreignField])
```

**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
Node.js: 16.11.0
Mongoose: 6.2.0

Contributor guide

Open the contributing guide

Research direction

Start in lib/helpers/populate/getModelsMapForPopulate.js around line 548, and read the surrounding populate mapping logic and the TypeScript declaration mentioned in the issue. Confirm how a string localField should match any field in an array foreignField, then verify that the existing populate behavior remains unchanged for string foreignField values.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, databases
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.