Member-only story
A Complete Guide to Building Nested Schemas in Nodejs & MongoDB Using Mongoose
Introduction
In complex applications, especially in education platforms or course management systems, data is often organized hierarchically. For example, a university course can contain multiple years, which have semesters, and each semester can have modules, lessons, and quizzes (MCQs). To efficiently store and manage such nested data structures, we can use Mongoose with MongoDB.

This article demonstrates how to build nested schemas using Mongoose and explains the process step-by-step, focusing on the example of a University Course Management System.
What is a Nested Schema?
A nested schema is a way to define complex structures inside a MongoDB document. It allows for multiple levels of sub-documents or arrays of objects, which are essential when working with relational data stored within the same collection. Instead of splitting the data across multiple collections, we embed it in the document as nested fields.
Example of Nested Schemas for a University Course
We will walk through a multi-level nested schema example where the following hierarchy is represented:
- Course