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 MongoDB Nested Query and some of the important aspects of nested queries as well.
What is MongoDB?
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
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.
Hevo Data offers a powerful, no-code ETL solution that supports MongoDB as a source, enabling you to integrate, transform, and load data seamlessly. With Hevo, managing data pipelines is simplified, allowing you to focus on gaining insights rather than worrying about data complexities.
Check out what makes Hevo amazing:
- It has a highly interactive UI that is easy to use.
- It streamlines your data integration task and allows you to scale horizontally.
- The Hevo team is available around the clock to provide exceptional support to you.
Streamline your data management with Hevo. Join our 2000+ happy customers. Check out what Hornblower and Deliverr have to say about us.
Get Started with Hevo for Free
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.
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:
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).
Integrate MongoDB to Redshift
Integrate MongoDB to BigQuery
Integrate MongoDB Atlas to MySQL
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” } )
Migrate Your MongoDB Data Seamlessly with Hevo
No credit card required
Conclusion
In this article, we discussed MongoDB Nested Query and various methods to perform the Nested query in MongoDB. We have covered the different operations with examples.
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, allowing you to focus on other aspects of your business, such as 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. Sign up for a 14-day free trial and experience the feature-rich Hevo suite firsthand.
FAQs
1. What is nesting in MongoDB?
Nesting in MongoDB refers to storing documents within other documents creating hierarchical or embedded structures to represent related data efficiently.
2. How do you create a nested query?
Use dot notation in MongoDB queries to access and filter data within nested fields. For example: { “address.city”: “New York” }.
3. Can a MongoDB query use multiple indexes?
Yes, MongoDB can use multiple indexes in a single query by leveraging index intersection and optimizing query performance.
Kamya is a dedicated data science enthusiast who loves crafting comprehensive content that tackles the complexities of data integration. She excels in SEO and content optimization, collaborating closely with SEO managers to enhance blog performance at Hevo Data. Kamya's expertise in research analysis allows her to produce high-quality, engaging content that resonates with data professionals worldwide.