Querying Embedded Documents in MongoDB: Complete Guide

Querying Embedded Documents Featured Image

This amateurs’ guide talks about querying embedded documents in MongoDB today.

The topic that we are interested in today is about querying embedded documents in MongoDB. Documents can absorb different levels of complexity. Usually, a complex database structure involves multiple collections populated across each other along with comprising embedded documents.

This complex structure sometimes results in difficulty in managing data. Individuals, especially ones who are new to programming and MongoDB may even have a hard time in the beginning. Luckily, there’s a way out.

This guide will help you in querying embedded documents in MongoDB regardless of how big the data your database contains, is. I will elucidate with multiple examples to help you learn querying embedded documents in MongoDB. So, let us get this beginners’ tutorial started!

Examples on Querying Embedded Documents in MongoDB

So, let us get started by working on a few examples to understand querying embedded documents in MongoDB better.

  • Start up the Mongo service
  • Choose from the databases you have available and navigate into one of them:
show dbs
use droneStore
  • Let us have a look at the documents in our collection first, to understand querying embedded documents in MongoDB:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Delivery",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Land Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Videography"
                ]
        }
}

Okay, so now we have listed all documents contained in the collection. You must have noticed the documents comprise fields with string values, numerical values, object values, and even arrays inside them.

Since this guide talks about querying embedded documents, I will discuss how to access data residing in embedded arrays and objects. So, let us dive deeper into the guide.

Querying Embedded Documents (Object Fields) in MongoDB

Let us query a nested document or a nested/embedded object field value.

> db.drones.find({"additionalDetails.material": "lithium"}).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Water Inspection"
                ]
        }
}

You might have noticed that I am using a ‘.’ dot here to access the embedded field. This is referred to as the dot notation in MongoDB.

The dot notation is used when you want to access data of fields that are situated in an embedded document.

Querying Embedded Arrays Fields in MongoDB

We will now query on a nested array field in this part of the guide on querying embedded documents in MongoDB.

> db.drones.find( { "additionalDetails.moreUses" : [ "Land Inspection", "Videography" ] } ).pretty()

{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Videography"
                ]
        }
}

Here, I have again made use of the dot notation. However, to get access to the array field’s data, I passed the values into an array. This way, you may even use operators for querying embedded documents in MongoDB.

Read More: Ultimate Guide to Customizing Flash Messages in Nodejs with Bootstrap

Conclusion

MongoDB is a cutting-edge database management system with many features and capabilities. Because of its ease of use and strong features, MongoDB is popular among novices.

The topic that we discussed today is about querying embedded documents in MongoDB. Documents can absorb different levels of complexity. Usually, a complex database structure involves multiple collections populated across each other along with comprising embedded documents.

This complex structure sometimes results in difficulty in managing data. individuals, especially ones who are new to programming and MongoDB may even have a hard time in the beginning. Luckily, there’s a way out.

This guide will help you in querying embedded documents in MongoDB regardless of how big the data your database contains, is. I will elucidate with multiple examples to help you learn querying embedded documents in MongoDB.

Noteworthy References

MongoDB