MongoDB $exists Operator: Ultimate Guide for Beginners

$Exists Operator In MongoDB Featured Image

In this ultimate beginner’s tutorial, I am going to discuss the $exists operator in MongoDB.

If you have ever been to the MongoDB documentation, you must have noticed how impressive is the list of operators MongoDB comes with. It allows developers to make their applications more functional, easy-to-use, and advanced.

These operators as well as their syntaxes and documentations are comprehensible even for beginners and starters of MongoDB. When I first started with MongoDB, I can say the experience was and still is amazing. Most importantly, I must say, I cannot forget most of what I learned mainly due to the simple structure they have adopted.

Today, the $exists operator in MongoDB is of our interest. The $exists operator allows us to check whether or not a given field exists in our MongoDB documents. It accepts a Boolean value for the query. If the Boolean value is true, then it will return us documents that contain the given field. Note that it will also return those documents that have the field value as “null”.

If the Boolean value is false, then it will return us only those documents that do not contain the given field.

In this beginner’s guide, I am going to cite an example for you to explain the $exists operator in a lucrative way. For this guide, I will be using a sample database with a few documents added inside a collection.

So, let us get started.

Finding Fields in Documents using $exists Operator with True Boolean Value

To start off, I will go with using the $exists operator and set the Boolean value as true to lookup fields that do exist inside documents.

Follow the measures below to get started:

  • Start you MongoDB shell server
  • Pass the below commands to list all the available databases and move into one of them:
show dbs
use droneStore
  • Now, first let us pretty print all our documents in our collection:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("603502ba24df1c2350e676e9"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "RX - Ferezza S120",
        "price" : 105499,
        "weight" : "26 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("60352ecb6ce2811cc8892a75"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "SV - LaserX AW205",
        "price" : 4000,
        "__v" : 0
}
{
        "_id" : ObjectId("6157239d341d653bccaf7822"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "DF - Jetfire Nitro RX-V",
        "price" : 22000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("6157240c341d653bccaf7823"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Pink Sentinel Q95",
        "price" : 13000,
        "__v" : 0
}
{
        "_id" : ObjectId("61573885341d653bccaf7825"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "SV - Xorvia 9908Y",
        "price" : 7760,
        "weight" : "3.8 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615738d6341d653bccaf7826"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "SV - Ryee SW657",
        "price" : 8900,
        "weight" : "760 grams",
        "__v" : 0
}
{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "RV - Zereca Eagle-i",
        "price" : 3200,
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "OS - Falcon GO!",
        "price" : 45000,
        "weight" : "17 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "DX - Firebird Scarlet Red",
        "price" : 45000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "IJ - Optico X53",
        "price" : 3900,
        "__v" : 0
}

  • You might have noticed some of our documents do not have the “weight” field. Well, let us assume it was later that we decided toad the “weight” field and we created some documents after that.

Let’s say we want to see only those documents that have the “weight” field. We shall use the $exists operator here for that purpose.

> db.drones.find( { weight: { $exists: true } } ).pretty()

{
        "_id" : ObjectId("603502ba24df1c2350e676e9"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "RX - Ferezza S120",
        "price" : 105499,
        "weight" : "26 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("6157239d341d653bccaf7822"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "DF - Jetfire Nitro RX-V",
        "price" : 22000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61573885341d653bccaf7825"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "SV - Xorvia 9908Y",
        "price" : 7760,
        "weight" : "3.8 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615738d6341d653bccaf7826"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "SV - Ryee SW657",
        "price" : 8900,
        "weight" : "760 grams",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "OS - Falcon GO!",
        "price" : 45000,
        "weight" : "17 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "DX - Firebird Scarlet Red",
        "price" : 45000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}

See? We successfully found six documents that have the “weight” field with the help of the $exists operator in MongoDB.

Finding Fields in Documents using $exists Operator with False Boolean Value

Now, I will be using the $exists operator and set the Boolean value as false to lookup fields that do not exist inside documents.

Simply pass the below command to use the $exists operator with a false Boolean value:

> db.drones.find( { weight: { $exists: false } } ).pretty()

{
        "_id" : ObjectId("60352ecb6ce2811cc8892a75"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "SV - LaserX AW205",
        "price" : 4000,
        "__v" : 0
}
{
        "_id" : ObjectId("6157240c341d653bccaf7823"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Pink Sentinel Q95",
        "price" : 13000,
        "__v" : 0
}
{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "RV - Zereca Eagle-i",
        "price" : 3200,
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "IJ - Optico X53",
        "price" : 3900,
        "__v" : 0
}

Hence, we have successfully found four documents that match our query using the $exists operator in MongoDB.

Read More: How to Connect MongoDB on Localhost 27017 in Windows – Starter’s Guide

Conclusion

Learn to use the $exists operator in MongoDB and the different ways you can use it to customize your Mongo shell query.

Noteworthy References

MongoDB Docs