relations Quick Start Guide (5 minutes)
Prerequisite knowledge: Understand the basics of MongoDB (collections, fields, foreign keys)
⚡ 1 minute to understand relations
What are relations?
- Define relationships between collections
- Use
populate()to automatically fill in associated data - Avoid writing multiple manual queries and merging results by hand
Core Concepts (only 3):
- from: related collection name (such as
'profiles') - localField: local foreign key field (such as
'profileId') - foreignField: foreign primary key field (usually
'_id')
Relationship with MongoDB $lookup:
- relations configuration = simplified version of MongoDB
$lookup - No need to learn new concepts, just understand MongoDB
⚡ 2 minutes to complete first example
Step 1: Define the relationship (30 seconds)
Step 2: Use populate (30 seconds)
Step 3: Done!
It's that simple, no need to write it manually:
⚡ 2 minutes to master common scenarios
Scenario 1: one-to-one
Example: A user has a profile
Scenario 2: one-to-many (one-to-many - reverse)
Example: A user has multiple articles (via reverse query)
Scenario 3: Multiple relationships
🎯 Configuration field quick check
💡 Quick decision-making
Q: Should I use single: true or single: false?
Decision Tree:
Q: What should I write in from?
Rule: Write the collection name (plural form)
⚠️ 3 common mistakes
Mistake 1: Use Model name instead of collection name
Mistake 2: Forgot to create an index
Mistake 3: single is used backwards
🚀 Next step
Have you mastered the basic functions? View advanced topics
- Select fields:
.populate('profile', { select: 'bio avatar' }) - Sort and limits:
.populate('posts', { sort: { createdAt: -1 }, limit: 10 }) - Nested populate: populate relations of already populated documents
- Performance: add indexes for foreign keys and use
$lookupdirectly for fully custom aggregation - Caching: cache read queries explicitly when the relation result is safe to reuse
Continue with the full documentation
📚 Sample Code Library
Complete example: Blog system
Complete sample file: populate relations runnable example
💬 FAQ
Q: Will populate affect performance?
A:
- Populate batches related IDs and avoids the common N+1 query pattern.
- Direct MongoDB
$lookupis still the right tool when you need a fully custom aggregation pipeline. - Recommendation: Create indexes for foreign keys + enable caching
Q: Can I associate a collection without defining a Model?
A:
- Yes for first-level populate.
fromcan point directly to a MongoDB collection name. - Define a Model for the related collection when you need schema validation, hooks, virtuals, or nested populate on that related data.
Q: How to deal with circular references?
A:
- The system automatically detects circular references and prevents infinite recursion
- Nested populate supports depth control; keep relationship graphs shallow and set explicit limits for deep branches.
Q: What is the difference with MongoDB $lookup?
A:
- relations are a simplified
$lookup-style configuration for common document references - populate batches related IDs and avoids the common N+1 query pattern
- use MongoDB
$lookupdirectly when you need a fully custom aggregation pipeline
Next steps
You now have the core relation setup:
- relation fields:
from,localField,foreignField,single - one-to-one and one-to-many examples
- common mistakes around collection names, indexes, and
single
Use the linked runnable example as the next reference when wiring relations into an application.
Need help?
- View the API reference
- Open an issue: https://github.com/vextjs/monSQLize/issues