MongoDB Nested Query: A Comprehensive Guide 101

By: Published: June 6, 2022

MongoDB Nested Query - FI

In today’s world exponentially growing unstructured data is the new normal thing for business. Managing and getting insights from unstructured data is challenging for a growing organization. According to research, the volume of unstructured data is larger than the volume of structured data at play. Traditional data handling methods such as Relational Database Management Systems (RDBMS) aren’t recommended while handling semi-structured or unstructured data. This is where Document-oriented, Open-source NoSQL Databases, such as MongoDB come to the rescue. 

Querying is a basic database management system operation that can modify, retrieve, insert, and delete data. This article aims to discuss a step-by-step guide on how to perform a Nested Query in MongoDB and some of the important aspects of nested queries as well.

Table of Contents

What is MongoDB?

MongoDB Logo : MongoDB Nested Query
Image Source

MongoDB is an open-source document-oriented NoSQL database. It is developed and maintained by MongoDB.Inc under the Server Side Public License (SSPL) and was first released in February 2009. MongoDB was designed to handle large volumes of data efficiently. It also supports data storage and retrieval in the document or key-value format. 

It is available as Cloud as well as on-primes i.e Server services. MongoDB offers both an Enterprise and Community version of its powerful distributed document database.

MongoDB’s document model is quite famous in the developer community as it provides all the capabilities needed to meet the most complex requirements at any scale. It provides drivers for 10+ languages including C++, Java, Ruby,Node.js, Perl, C#, Scala, and other programming languages as well.

Key Features of MongoDB

Key features of MongoDB: MongoDB Nested Query
Image Source

The key features of MongoDB are as follows:

  • High Performance: MongoDB is known for its high performance and data persistence as compared to other databases.
  • Aggregation: This feature enables you to perform operations on the collection of documents and get a single aggregated result. 
  • Replication: Maintaining multiple copies of documents and collections ensures high performance and low downtime in MongoDB. 
  • Schema-less Database: As a NoSQL database, MongoDB follows a no-schema utility. A schema-less database is defined as a collection of multiple documents that may differ in the schema. There is no compulsion to predefine schema for collection or documents in MongoDB.
  • Document Oriented: Instead of storing data in tabular format, data is stored in key-value pair format. MongoDB leverages the BSON format to store data.
  • Sharding: To accomplish the availability feature, data is being distributed among various physical partitions called shards. MongoDB has build-in automatic sharding and load-balancing capabilities.
  • GridFS: This feature allows you to split a file into smaller pieces and save them in different documents without complicating the batch. It also facilitates an increase in Data Availability while using MongoDB. 
  • Indexing: All data resides in documents and collections are indexed in MongoDB. This feature ensures a high response time while querying the database.

What is Nested Query?

Nested queries or Embedded queries are those queries that have another query embedded in them. For nested queries, the internal query is executed only once at the beginning. The outer query is executed using the results of the inner query. Therefore, the internal query is used when executing the external query.

Replicate Data From MongoDB in Minutes Using Hevo’s No-Code Data Pipeline

Hevo Data, a Fully-managed Data Pipeline platform, can help you automate, simplify & enrich your data replication process in a few clicks. With Hevo’s wide variety of connectors and blazing-fast Data Pipelines, you can extract & load data from 150+ Data Sources such as MongoDB straight into your Data Warehouse or any Databases. To further streamline and prepare your data for analysis, you can process and enrich raw granular data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!

GET STARTED WITH HEVO FOR FREE

Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

What is MongoDB Nested Query?

MongoDB delivers an upstanding feature named Embedded or Nested Document. An Embedded or Nested Document is a type of document that contains one document within another. That is if a collection contains one document, that document contains another document, another document contains another child document, and so on, such document types are called embedded/nested documents.

A few points to note here:

  • Nesting up to level 100 is possible in MongoDB.
  • The overall document size in nested queries should be within a limit of 16MB.

Understanding MongoDB Nested Query Aspects

MongoDB database doesn’t support sub queries or joins. Embedded or Nested Query feature is available in MongoDB to get joins or sub queries functionality.   

Creating an Embedded/Nested Document

MongoDB makes it easy to embed a document in another document. As you know, the mongo shell documentation is defined by curly braces ({}), and inside these curly braces are field/value pairs. 

 You can use curly braces {} to embed or set another document within these fields. This document can include field-value pairs or other partial documents.

Syntax:

{  
field: { field1: value1, field2: value2, field3: value3 }
}

Example of Nested Query in MongoDB:

There is a “company” database as shown in the following image. This database now has a collection named “Inventory” that contains the following documents: 

MongoDB Nested Query: Example Illustration
Image Source

The following nested query will insert into the inventory collection:

db.inventory.insertMany( [
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);

You can notice in the inventory collection you have 5 documents that contain the nested document. You can retrieve these nested documents by using db.collection.find() method in mongosh. 

 This document has a field name “item” that contains another document, and this document contains two fields with values ​​(that is, sku, color).

What Makes Hevo’s ETL Process Best-In-Class?

Providing a high-quality ETL solution can be a difficult task if you have a large volume of data. Hevo’s automated, No-code platform empowers you with everything you need to have for a smooth data replication experience.

Check out what makes Hevo amazing:

  • Fully Managed: Hevo requires no management and maintenance as it is a fully automated platform.
  • Data Transformation: Hevo provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Faster Insight Generation: Hevo offers near real-time data replication so you have access to real-time insight generation and faster decision making. 
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 150+ sources (with 40+ free sources) that can help you scale your data infrastructure as required.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-day free trial!

MongoDB Nested Query Match Types

You can use the query filter document { <field> : <value>}  to specify an equality condition on field. <field> referes to the field present in document and <value> talks about the document to match.

The equality condition will look for embedded documents that need an exact match with the specified document, including the order of the fields.

For example, the following query selects all documents where the field size equals the document { h: 14, w: 21, uom: “cm” }:

Query:

db.inventory.find(  { size: { w: 21, h: 14, uom: "cm" } }  )

MongoDB Nested Query Match on a Nested Field

You can use the dot notation (“field.nestedField”) to specify query criteria for fields in embedded/nested documents. For queries that use dot notation, fields and nested fields must be enclosed in double-quotes. 

This is what the syntax would look like:

{
 	 { <field.nestedField> : <value>} 
}

For example: 

db.inventory.find( { "size.uom": "in" } )

The above example selects all documents whose size field nested field uom is equal to “in”.

MongoDB Nested Query Match Using Query Operator

In query filter documents, you can use query operators to specify conditions of the form of the following syntax:

Syntax: 

{ <field1>: { <operator1>: <value1> }, ... }
  • Query using the Less Than operator ($lt) on the Field:

MongoDB provides $lt to select documents whose field values ​​are less than the defined values. This applies to documents that do not contain fields.

Syntax:

{ field: { $lt: value } }

Example: db.inventory.find( { “size.h”: { $lt: 15 } } )

The above query will return all the documents where “size.h”less than 15.

  • Query using the Greater Than Operator ($gt) on the Field:

Similar to the above discussed less-than operator, you can use the greater than the operator in a MongoDB nested query. $gt operator is used to select documents whose field values are greater than the defined values.

Syntax:

{ field: { $gt: value } }

Example: db.inventory.find( { “size.h”: { $gt: 55 } } )

The above query will return all the documents where “size.h” greater than 55.

  • Query using the Not Equal operator ($ne) on the Field:

MongoDB provides $ne to select documents whose field values ​​are not equal to the defined values. This applies to documents that do not contain fields.

Syntax:

{ field: { $ne: value } }

Example: db.inventory.find({ “item.color”: { $ne: “blue”} })

The above query will return all the documents where item color is not blue.

MongoDB Nested Query Match Using Query Operator AND Condition

You can use the following query to select all documents whose nested field h is  less than 15, the nested field uom equals “in”, and the status field equals “D”:

Example: 
db.inventory.find( { “size.h”: { $lt: 15 }, “size.uom”: “in”, status: “D” } )

Conclusion

In this article, we discussed MongoDB Nested Query and various methods to perform the Nested query in MongoDB. And, we have covered the different operations with examples.

Visit our Website to Explore Hevo

MongoDB is a great tool for storing your business data. However, at times, you need to transfer this data to a Data Warehouse for further analysis. Building an in-house solution for this process could be an expensive and time-consuming task. Hevo Data, on the other hand, offers a No-code Data Pipeline that can automate your data transfer process, hence allowing you to focus on other aspects of your business like Analytics, Customer Management, etc. This platform allows you to transfer data from 150+ Data Sources like MongoDB to Cloud-based Data Warehouses like Snowflake, Google BigQuery, Amazon Redshift, etc. It will provide you with a hassle-free experience and make your work life much easier.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. 
You can also have a look at our unbeatable Hevo Pricing that will help you choose the right plan for your business needs!

Share your views on MongoDB Query Nested object in the comments section!

Kamya
Former Marketing Analyst, Hevo Data

Kamya is a data science enthusiast who loves writing content to help data practitioners solve challenges associated with data integration. He has a flair for writing in-depth articles on data science.

No Code Data Pipeline For MongoDB