Automattic / Automattic/mongoose
Population options
- Dominant language
- JavaScript
- Stars
- 27.5k
- Forks
- 4k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 35
Description
I think it would be convenient to add some customisations for the `populate()` function.
For example, developer can save a reference from one schema to another using a property name (current behavior):
``` javascript
bookSchema = mongoose.Schema({
name: {type: String},
author: {type: mongoose.Schema.Types.ObjectId, ref: 'Author'}
});
authorSchema = mongoose.Schema({
fullName: {type: String}
});
Book = mongoose.model('Book', bookSchema);
Author = mongoose.model('Author', authorSchema);
Book.findOne().populate('author').exec(function (err, book) {
console.log(book); // => {
// _id: 'asdfgi345iq2l3jk34',
// name: 'Some title',
// author: {
// _id: '23k5h02938409235sdf',
// fullName: 'John Doe',
// }
// }
}
```
Or developer can save and _id inside a property and call a population function with additional parameter (for example `replace`, which would be `false` by default)
``` javascript
bookSchema = mongoose.Schema({
name: {type: String},
author: {
_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Author'}
}
});
authorSchema = mongoose.Schema({
fullName: {type: String}
});
Book = mongoose.model('Book', bookSchema);
Author = mongoose.model('Author', authorSchema);
Book.findOne().populate({path: 'author._id', replace: true}).exec(function (err, book) {
console.log(book); // => {
// _id: 'asdfgi345iq2l3jk34',
// name: 'Some title',
// author: {
// _id: '23k5h02938409235sdf',
// fullName: 'John Doe',
// }
// }
}
```
And the result will be the same.
I think that the new method will allow to store and retrieve data in more natural manner.
Contributor guide
Research direction
The issue concerns customization of Mongoose's populate() function and gives two schema/query examples using nested ObjectId references. Review the existing populate() API and its tests first; done would require an agreed option and behavior that produces the demonstrated populated result. No source files or test paths are named in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, node.js
- Domain
- backend, database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100