Embedded Documents in MongoDB (original) (raw)

Last Updated : 5 May, 2026

MongoDB’s Embedded (Nested) Documents allow you to store documents inside other documents, enabling hierarchical data structures and provides faster access to related data, especially in read-heavy workloads where related data is frequently accessed together within a single query.

database_geeksforgeeks

**Notes - In MongoDB, you can only nest documents up to 100 levels. The overall document size must not exceed 16 MB.

Creating Embedded Documents

Creating embedded documents involves structuring related data as sub-documents within a parent document using nested fields.

**Syntax:

db.collection.insertOne({
field1: value1,
field2: value2,
embeddedField: {
subField1: value1,
subField2: value2,
nestedField: {
innerField1: value1
}
}
})

**Example 1: In the GeeksforGeeks database, the Courses collection stores a document where the name field is an embedded document containing first, middle, and last fields.

Screenshot-2026-02-21-093915

**Example 2: In the GeeksforGeeks database, we add another document to the Courses collection where courseDetails is an embedded document containing course info and a nested paymentDetails sub-document.

Screenshot-2026-02-21-094215

So, overall the Courses collection contains two documents and these documents contain nested documents.

Screenshot-2026-02-21-094324