Skip to content

A mongoose plugin to populate into nested documents in depth by a easy reading path

License

Notifications You must be signed in to change notification settings

radiotommy/mongoose-dive-populate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoose-dive-populate

A mongoose plugin to populate into nested documents in depth by a easy reading path. The path can be specified by a url like: "parent/children"

Can also select fields of population like: "parent{name age}/children{name toy}"

Can limit the number of subdocuments in population "parent{name age}/children[5]{name}"

Usage

Register plugin

const divePopulate =require('mongoose-dive-populate')
mongoose.plugin(divePopulate)

or:

YourSchema.plugin(divePopulate)

population Example:

const Club = mongoose.model('Club', new mongoose.Schema({
    name: String,
    players: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Player'
    }],
    city: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'City'
    }]
}))

const Player = mongoose.model('Player', new mongoose.Schema({
    name: String,
    age: number,
    goals: number,
    fans: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Player'
    }]
}))

const Fan = mongoose.model('Player', new mongoose.Schema({
    name: String
}))

Club.findOne({name: 'Barcelona'})
    .diveTo('players/fans')

// Can select fields
Club.findOne({name: 'Barcelona'})
    .diveTo('players{name}/fans')

// Can use with normal populate together 
Club.findOne({name: 'Barcelona'})
    .diveTo('players{name}/fans')
    .populate('city')

// Can select fields
Club.findOne({name: 'Barcelona'})
    .diveTo('players{name goals}/fans{name}')

// Can exclude fields
Club.findOne({name: 'Barcelona'})
    .diveTo('players{-age -goals}/fans{name}')

About

A mongoose plugin to populate into nested documents in depth by a easy reading path

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published