Source: Dev

MongoDB: Best Practices for Designing Scalable MongoDB Models with Mongoose

Creating complex models in MongoDB using Mongoose requires careful planning to ensure scalability, maintainability, and efficiency. Here are the best practices for designing complex models in MongoDB with Mongoose . 1. Schema Design Best Practices ✅ Use Embedded Documents for One-to-Few Relationships If the related data is small and read together frequently, embed it inside the document. Example: A User with multiple addresses ✔ Pros: Faster read operations, fewer queries ❌ Cons: Updates require writing the entire document again Use when: Data is frequently read together The number of embedded documents is small (10) ✅ Use References (Normalization) for One-to-Many Relationships If the related data is large or frequently updated separately, store references (ObjectIds). Example: A User with multiple Orders (large dataset) ✔ Pros: Efficient updates, avoids document bloat ❌ Cons: Requires populate() to fetch related data Use when: The sub-collection grows large (>10 items) You need independent CRUD operations on the sub-collection 🔹 Fetching referenced data with populate: ✅ Hybrid Approach (Partial Embedding + References) For medium-sized related data, embed only frequently used fields and reference the rest. Example: Embed order summary but reference order details ✔ Best of both worlds - fast reads and efficient updates 2. Schema Design Optimizations ✅ Indexing for Fast Queries Indexes improve query speed. Always index fields that are frequently queried. ✔ Use indexes on: Frequently queried fields ( email , username ) Fields used in sorting ( createdAt ) Fields used in filtering ( status , category ) 🔹 Check Index Usage ✅ Timestamps for Tracking Use timestamps: true in your schema to automatically store createdAt and updatedAt . ✅ Use lean() for Read-Only Queries lean() improves performance by returning plain JavaScript objects instead of full Mongoose documents. ✔ 30-50% faster than normal queries Use when: You don't need to modify the retrieved data You only need raw JSON output for API responses 3. Handling Large Data Efficiently ✅ Pagination for Large Datasets Use pagination to limit query results for better performance. ✔ Avoid limit(1000) , as it can cause performance issues ✅ Aggregation Pipeline for Complex Queries Use aggregation for reporting and complex queries. 4. Soft Deletes Instead of Permanent Deletion Instead of deleting a document, use a deletedAt field . ✔ Hides deleted items without losing data 🔹 Query only active users: 5. Virtual Fields for Computed Values Virtual fields do not get stored in the database but are calculated dynamically. ✔ Use for derived data without increasing DB size Conclusion 🚀 Best Practices Summary ✅ Embed small data, reference large data ✅ Use lean() , pagination, and caching ✅ Index frequently queried fields ✅ Use soft deletes instead of actual deletion ✅ Use environment variables for security ✅ Use middleware for automation Following these practices will help you build efficient, scalable, and maintainable MongoDB applications with Mongoose! 🚀

Read full article »
Annual Revenue
$1.0-5.0B
Employees
5.0-10K
Dev Ittycheria's photo - President & CEO of MongoDB

President & CEO

Dev Ittycheria

CEO Approval Rating

83/100

Read more