Back to: MongoDB Tutorials
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.
MongoDB is a schema-less database that does not require any pre-defined schema like a relational database. So, in MongoDB, the data is stored in the document in the form of key-value pairs, and further, the document is present inside the collection, and the collection is present inside the database. Besides the schema-less feature, MongoDB also provides a unique feature that is embedded or nested document. If a document is present inside another document then such types of documents are known as nested or embedded documents. An embedded document can also contain another document and so on. But you cannot exceed nest documents up to 100 levels and the document size must not exceed 16MB.
Create Embedded Documents in MongoDB
As we know, we create documents using curly braces({}) and keep our key-value pairs inside these braces. Similarly, we can create an embedded document inside a field of another document. In the field, we use curly braces ({}) to store the key-value pairs of the embedded document, it also contains another embedded document.
Syntax:
{
…
Field:{field1:value1, field2:value2, field3:value3}
….
}
Example 1:
db.embeddedColl.insertOne({ name: “Sunita”, Articles:{lang1:”C#”, count:45, lang2:”Java”, count:87, lang3:”C”, count:78}, Branch: “CSE”, PassoutYear: 2013 })
Here in the above example, we create a nested document inside the Articles field using curly braces{}. The nested document contains four field value pairs that are:
Articles:{lang1:”C#”, count:45, lang2:”Java”, count:87, lang3:”C”, count:78}
Output:
Example 2:
> db.embeddedColl.insertOne({
… name: “Amit”,
… Articles:{lang:”C#”, count:45, type:{program:10, function:13, concept:22}},
… year:2015
… })
Here, in the above example, we create a nested document inside the Articles field, and inside this nested document, we create another nested document inside the type field using Curley braces{}.
Output:
Query on Embedded Document in MongoDB
To perform the query on the embedded document we use the find() method. Using the find() method we can perform equality match embedded document or nested field, etc.
Perform Equality Match on the Embedded Document
To perform equality match on the embedded document we use query filter document{field: Value} where value is the document to match. Always remember in the equality match operation to the whole document requires an exact match of the specified value document including the order of the fields.
Example:
db.embeddedColl.find({Articles:{lang1:”C#”, count:45, lang2:”Java”, count:87, lang3:”C”, count:78}}).pretty()
Output:
Perform Query on the Nested Field
To perform a query on the field of the nested document we use dot notation. We can also use operators and conditional operators with nested fields.
Syntax: “field.nestedfield”
While querying on the nested field the field and the nested field should be inside quotation marks.
Example: db.embeddedColl.find({“Articles.lang”:”C#”}).pretty()
Here in the above example, we display all the documents whose nested field(Articles.lang) value is C#.
Output:
Example:
db.embeddedColl.find({“Articles.count”:{$gt:50}}).pretty()
In the above example, we display all the documents whose nested field(Articles.count) value is greater than 50. Here we use $gt operator for greater than operation.
Output:
An Array of Embedded Documents in MongoDB
In MongoDB, we are allowed to create an array of embedded documents. An array of embedded documents means a field can contain an array of embedded documents. To create an array use brackets([]) and here each embedded document is separated by a comma.
Syntax:
{
Field:[{nestField1:value1, nestField2:value2}, {nestField1:value1, nestField2:value2}]
}
Example:
db.embeddedColl.insertOne({ name:”Poonam”, Articles:[{language: “Python”, Count: 34}, {language:”C#”, Count: 45}],year: 2013})
Here, the Articles field contains an array of nested documents.
Output:
Query an Array of Embedded Documents in MongoDB
To perform the query on the array of embedded documents we use the find() method. Using the find() method we can perform equality match embedded documents or nested fields, etc. In the below examples, we use the following documents:
Perform Equality Match on the Array of the Embedded Document
To perform equality match on the array of embedded documents we use query filter document{field: Value} where value is the document to match. Always remember in the equality match operation to the whole document requires an exact match of the specified value document including the order of the fields.
Example: db.embeddedColl.find({Articles:{language: “Java”, Count: 78}}).pretty()
Output:
Perform Query on a Field in an array of Embedded Documents.
To perform a query on the field in an array of nested documents we use dot notation. Here simply concatenate the name of the array field with the dot operator and the name of the field in the nested document.
Syntax: “field.nestedfield”
While querying on the nested field the field and the nested field should be inside quotation marks.
Example: db.embeddedColl.find({“Articles.language”:”C#”}).pretty()
In the above example, we display all the documents whose nested field(Articles.language) value is C#.
Output:
Perform Query on a Field in an array of Embedded Documents using an Array Index
With the help of the dot operator, we can also specify the query condition for a field in a document at a particular index or position of the array. Here the array uses 0-based indexing.
Syntax: “field.0.nestedfield”
While querying on the nested field the field, index, and the nested field should be inside quotation marks.
Example: db.embeddedColl.find({“Articles.1.Count”:{$lte:50}}).pretty()
Here, in the above example, we select all the documents where the Articles array has its second element a document that contains field Count whose value is less than equal to 50. Here $lte operator is used for less than equal to.
Output:
Perform Multiple Conditions on the Array of Documents
We can also perform multiple conditions on the array of documents by specifying more than one condition on the field nested in an array of documents. You can set the condition in such a way either one or more documents(which contain an array of nested documents) can satisfy the given condition.
Example: db.embeddedColl.find({“Articles”:{$elemMatch:{Count:{$gt: 30, $lt:50}}}}).pretty()
Here in the above example, we select all the documents where the Articles array has at least one embedded document that contains the field Count whose value is greater than 30 and less than 50.
Output:
In the next article, I am going to discuss Updating Single Documents in MongoDB with Examples. Here, in this article, I try to explain Embedded Documents in MongoDB with Examples. I hope you enjoy this article.