Populate
Population is a powerful feature in Monarch ORM that allows you to replace foreign key references in your documents with the actual related documents from other collections. This is especially useful for retrieving related data in a single query, avoiding multiple database round trips.
Basic Population
The simplest way to populate a relation is to pass true
as the population option. This will replace the foreign key field with the related document(s).
Population Options
To control the behavior of the population process, you can pass an object with various options to the populate
method. These options allow you to customize which documents are retrieved, how they are sorted, and which fields are included or excluded.
1. limit
: Limiting the Number of Populated Documents
The limit
option restricts the number of documents returned for a one-to-many or reference relationship.
This will populate the posts
relation with a maximum of 5 Post
documents.
2. skip
: Skipping a Number of Populated Documents
The skip
option skips a specified number of documents before starting to populate the relation. This can be useful for pagination or retrieving a specific subset of related documents.
This will skip the first 10 Post
documents and then populate the posts
relation with a maximum of 5 documents.
3. sort
: Sorting Populated Documents
The sort
option allows you to specify the sorting order for the populated documents. This can be useful for ensuring that the related documents are returned in a specific order.
This will sort the Post
documents by the createdAt
field in descending order before populating the posts
relation. Use "asc"
or 1
for ascending order, "desc"
or -1
for descending.
4. select
: Including Specific Fields
The select
option lets you specify which fields to include in the populated documents. This can reduce the amount of data transferred and improve performance.
This will populate the posts
relation with Post
documents that only include the title
and contents
fields.
5. omit
: Excluding Specific Fields
The omit
option lets you specify which fields to exclude in the populated documents. This is the inverse of the select
option.
This will populate the posts
relation with Post
documents that exclude the secret
field.
Combining Population Options
You can combine multiple population options to achieve fine-grained control over the population process.
This will:
- Populate the
posts
relation with a maximum of 5Post
documents. - Sort the
Post
documents by thecreatedAt
field in descending order. - Include only the
title
andcontents
fields. - Exclude the
authorId
field.
Nested Population
Monarch ORM now supports nested population, enabling you to populate relations of related documents within a single query. You can achieve this by passing another populate
object as a parameter to a relation's populate
function. This offers a more efficient approach to retrieving deeply nested data.
In this setup the comment author is now also populated
Important Considerations
- Performance: Population can be expensive, especially for large datasets or complex relationships. Consider using projections (
select
andomit
) to reduce the amount of data transferred. - Schema Design: A well-designed schema with appropriate indexing can improve the performance of population operations.
Population offers a convenient way to retrieve related data in Monarch ORM. By understanding the various population options, you can tailor the population process to your specific needs and optimize performance. Now supports nested population!