Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
92 lines (67 sloc) 2.05 KB

Populating references

There are no joins in MongoDB but sometimes we still want references to documents in other collections. This is where population comes in.

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). We may populate a single document, multiple document.

Using references in a schema

The Type.Ref allows you to insert populatable references in your documents.

For example, consider a blog, where posts are stored in a collection, and comments for these posts are stored in another one; each comment should reference the parent post.

const commentSchema = new Schema({
    post: Type.Ref('Post', {
        validations: [
    author: Type.Ref('Author', {
        validations: [

class Comment extends Model(commentSchema, connection, 'Comment') {


Populating fields when querying

When fetching all comments posted by an author, we want to display the related blog articles:

        author: currentUser
    .then(function(comments) {


Populate field of an existing document

If the document has already been queried, we can populate a field:

.then(function(newComment) {
    // is a Post instance

Populating sub-documents

Populating also works for sub-documents and fields in iterables.

.then( ... );

Field selection

What if we only want a few specific fields returned for the populated documents? This can be accomplished by passing options as the second argument to the populate method:

comment.populate('user', {
    select: 'username'
.then( ... );

Query conditions

Population can also be limited by a query:

comment.populate('user', {
    match: { site_admin: true }
.then( ... );