MongoDB does not have joins but sometimes we want references to documents in other collections. For this we have to use population.
Population is the way of matching the specified paths in the document with document(s) from other collection(s).
Let's look at below examples.
Populating One Field
If we want to populate a single field in the query for an object, we specify the name of that field in a string to the populate function:
Player
.findOne({ _id: 'abc123' })
.populate('leagues')
.exec (err, player) -> #..
.Populating Multiple Fields
Suppose we want to populate a multiple fields in the query for an object,we use below method:
League
.find()
.populate('created_by players')
.exec (err, league) -> #..
cheers :)
0 Comment(s)