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.
Populating fields when querying
When fetching all comments posted by an author, we want to display the related blog articles:
Populate field of an existing document
If the document has already been queried, we can populate a field:
Populating sub-documents
Populating also works for sub-documents and fields in iterables.
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:
Query conditions
Population can also be limited by a query:
Last updated