https://github.com/oznakn/mongoose-plugin-deep-populate
Populate any field in mongoose schemas without thinking about depth
https://github.com/oznakn/mongoose-plugin-deep-populate
Last synced: 4 months ago
JSON representation
Populate any field in mongoose schemas without thinking about depth
- Host: GitHub
- URL: https://github.com/oznakn/mongoose-plugin-deep-populate
- Owner: oznakn
- License: mit
- Created: 2019-04-28T11:05:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-12-29T23:39:45.000Z (almost 4 years ago)
- Last Synced: 2025-02-22T05:48:03.421Z (8 months ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-plugin-deep-populate
DeepPopulate plugin helps you do some auto population without thinking about depth, inspired from [mongoose-autopopulate](https://github.com/mongodb-js/mongoose-autopopulate).
## Installation
Just run
```
npm install mongoose-plugin-deep-populate
```
or
```
yarn add mongoose-plugin-deep-populate
```And just call it from a model created JavaScript file
```javascript
const mongoose = require('mongoose');const schema = new mongoose.Schema({
...
});schema.plugin(require('./mongoose-plugin-deep-populate'));
```Or install it for all schemas
```javascript
const mongoose = require('mongoose');mongoose.plugin(require('./mongoose-plugin-deep-populate'));
```## Usage
Let's create a School model
```javascript
const SchoolSchema = new mongoose.Schema(
{
name: {
type: String
},
},
);const School = mongoose.model('school', SchoolSchema);
```And Student model
```javascript
const StudentSchema = new mongoose.Schema(
{
firstName: { type: String },
lastName: { type: String },
school: {
type: Mongoose.SchemaTypes.ObjectId,
ref: 'school',
},
},
);const Student = mongoose.model('student', StudentSchema);
```And ExamMark model
```javascript
const ExamMarkSchema = new mongoose.Schema(
{
student: {
type: Mongoose.SchemaTypes.ObjectId,
ref: 'student',
},
mark: {
type: Number,
}
},
);const ExamMark = mongoose.model('exammark', ExamMarkSchema);
```And the only think we have to do to use the deep population plugin is add the deepPopulate option field
```javascript
const ExamMarkSchema = new mongoose.Schema(
{
...
},
{
deepPopulate: {
student: {
school: true,
},
},
},
);
```Or you want to populate only one deep level, you can also do
```javascript
const ExamMarkSchema = new mongoose.Schema(
{
...
},
{
deepPopulate: ['student'],
},
);
```And just call
```javascript
const examMark = await ExamMark.findOne({ _id: '...' });
```And with doing that every time a query runs on ExamMark model, student and school of student fields automatically populated as nested. Now, deepPopulation only works on the queries on ExamMark model, since we add options to the only ExamMark model. Any query runs on Student model does not populate school field since we have not add the deepPopulation field to the student model.
___
To skip the deep population step, just use ```skipPopulation``` function
```javascript
const examMark = await ExamMark.findOne({ _id: '...' }).skipPopulation(true);
```Default value of the ```skipPopulation``` function parameters is ```true```, so you can also do
```javascript
const examMark = await ExamMark.findOne({ _id: '...' }).skipPopulation();
```