Embedded Documents in MongoDB

Embedded Documents in MongoDB with Examples

In this article, I am going to discuss Embedded Documents in MongoDB with Examples. Please read our previous article where we discussed Deleting Documents in MongoDB with Examples.

MongoDB Embedded Documents

In MongoDB, you are allowed to create an embedded document/ Nested document. Nested documents mean documents inside another document. Or we can say that an embedded document is a document that contains nested documents, nested documents can contain another sub-document, and so on. You are allowed to nest documents up to 100 level and the overall document size must not exceed 16 MB.

Create Embedded Document in MongoDB

Documents are created by using curly braces({}) and inside these curly braces, we store data in the field-value pairs. So, inside these fields, we can embed other documents with field-value pairs.

Syntax:
{
field1: {field1: value, field2:value}
field2: value
}

Example to Understand Embedded Documents in MongoDB

Here in the below example, we use dotnettutorial Database. Now in this database, we insert a document in the authors collection. Inside this document, we have a name field that contains a nested document with fName, mName, and lName field-value pairs.

use dotnettutorials
db.authors.insertOne({ name: {fName: “Sumit”, mName:”kumar”, lName: “Singh”},
… group: “D”,
… tutorial: “MongoDB”,
… articleNumber: 23
… })

Example to Understand Embedded Documents in MongoDB

Query on Nested/Embedded Document in MongoDB

To perform a query on the field of a nested document we use dot notation.

Syntax: {“fieldName.nestedFieldName” }

Example:

Here in the below example, we find all the documents in which the middle name of the author is “Kumar”.

db.authors.find({“name.mName”: “kumar”}).pretty()

Here we use dot notation to access the field of the embedded document.

Query on Nested/Embedded Document in MongoDB

An Array of Nested Documents in MongoDB

In MongoDB, we are allowed to create an array of nested documents. It means a single field can contain multiple embedded documents. The array is read by using bracket braces [] and each document is separated by a comma.

Syntax:
{
field1: [{field1:value, field2:value}, {field1:value, field2:value}, {field1:value, field2:value}]
field2: value
}

Example:

Here in the below example, the sale collection has one document. In this document, the course field contains an array of embedded documents.

db.sale.insertOne({
… courseNumber:3,
… type: “programming”,
… course:[{name:”mongoDB”, article:40}, {name:”C++”, article:50}, {name:”C#”, article:80}]
… })

Query on an array of Nested/embedded Documents

To perform a query on the field which contain an array of nested document or query on the nested document fields we use dot notation.

Syntax: {“fieldName.nestedFieldName”}

Query on an array of Nested/embedded Documents in MongoDB

Example:

Here in the below example, we display all the documents in which the course.name is mongoDB.

db.sale.find({“course.name”:”mongoDB”}).pretty()

Embedded Documents in MongoDB with Examples

In the next article, I am going to discuss Updating Single Document in MongoDB with Examples. Here, in this article, I try to explain Embedded Documents in MongoDB with Examples. I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *