Querying Arrays in MongoDB: 2021 Detailed Guide

In this beginners’ tutorial, we will learn querying arrays in MongoDB.

MongoDB is a notable record-arranged informational collection organization structure that licenses developers to store mammoth proportions of data. It is open-source and especially viable. Disregarding the way that it can sometimes get exceptionally puzzling for beginners when learning without an ODM, it has definite documentation.

It is a NoSQL type database that is it stores data in the BSON plan as reports inside arrangements.

The MongoDB informational index organization structure is moved by supervised by MongoDB Inc. It was set up in 2007 and was first dispatched in February 2009. The DBMS is directed under SSPL (Server-Side Public License).

MongoDB offers a combination of operators, functions, and methods that can be used for a variety of purposes. It has given computer programmers direct similarly as versatile chairmen that license them to do essentially more than CRUD in a MongoDB-maintained application. This is moreover one defense for why MongoDB is so notable.

With the help of operators and other features, you can customize querying, printing, editing, and anything is possible from that point. It has found being a fan even among tenderfoots close by senior developers.

This guide on querying arrays in MongoDB will provide multiple examples to explain the different ways of querying arrays in MongoDB. So, let us get started.

Querying Arrays in MongoDB with Examples

The upcoming subsections in this section on querying arrays in MongoDB will comprise of the below-listed types of examples that I would like to cover for you:

  • Exact matching an array
  • Querying for an element with a single condition
  • Querying with multiple conditions for an element

Querying Arrays in MongoDB – Exact Matching an Array

In this sub-section, I will explain how querying arrays in MongoDB functions by discussing exact matching an array in MongoDB works.

  • Start up the MongoDB server and open the Mongo shell
  • Choose the database you want to work in and navigate into it:
show dbs
use droneStore
  • Taking a look at the available documents in our database collection:
> 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"
                ]
        }
}
  • Now, we will query for an exact match in an element of an array:
> db.drones.find( { utility: [
...                 "Delivery",
...                 "Combat",
...                 "Rescue",
...                 "Construction"
...         ] } ).pretty()


{
        "_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"
                ]
        }
}

Querying Arrays in MongoDB – Using a Single Condition

In this sub-section, I will explain how querying arrays in MongoDB functions by discussing how to match an array using a single condition in MongoDB.

> db.drones.find( { $or: [ { utility: [
...                 "Photography",
...                 "Combat",
...                 "Rescue",
...                 "Construction"
...         ] }, { "additionalDetails.moreUses": [
...                         "Land Inspection",
...                         "Videography"
...                 ] } ] } ).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"
                ]
        }
}
{
        "_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"
                ]
        }
}

Querying Arrays in MongoDB – Using Multiple Conditions

In this sub-section, I will explain how querying arrays in MongoDB functions by discussing how to match an array using multiple conditions in MongoDB.

> db.drones.find( { utility: {$eq: [
...                 "Photography",
...                 "Combat",
...                 "Rescue",
...                 "Construction"
...         ], $ne: [
...                 "Videography",
...                 "Combat",
...                 "Rescue",
...                 "Construction"
...         ] } } ).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"
                ]
        }
}

This way, you can perform querying arrays in MongoDB.

Read More: Comprehensive Guide to Using the $ne Operator in MongoDB

Conclusion

In this article, we learned how to perform different queries on Arrays.

Noteworthy References

MongoDB

Aneesha S
Aneesha S
Articles: 173