MongoDB $exists Operator: Check if a Field Exists

If you’ve ever gone through the MongoDB documentation, you must have noticed how impressive the list of operators that comes with MongoDB is. It allows developers to make their applications more functional, easy to use and advanced. These operators as well as their syntax and documentation are understandable even for MongoDB beginners. One of them is the $exists operator.

The $exists operator allows us to check the existence of the field in documents of a MongoDB collection. It accepts a boolean value for a query. If the boolean value is true, it will return the document that contains the given fields. It will also return elements that have a field value of “null”.

If the boolean value is false, it will only return documents that do not contain the specified field.

In this beginner’s guide, we’ll give you several examples to illustrate the $exists operator in an engaging way. For this guide, we’ll use a sample database with a few documents added inside a collection.

Check if a field exists in a MongoDB Collection using exists Operator with True Boolean Value

To start off, let’s use the $exists operator and set the Boolean value as true to look up some specific fields that do exist inside documents.

Follow the measures below to get started:

  • Start the MongoDB shell server
  • Pass the below commands to list all the available databases and move into one of them, for example:
show dbs
use droneStore
  • Now, first, let us pretty print all the documents of a selected 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 noticed that not every document has a “Weight” field.

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.

Check if a field exists in a MongoDB Collection using exists Operator with False Boolean Value

Now, let’s use the $exists operator and set the Boolean value as false to look up 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
}

The return document does not have the “weight” field. Hence, we have successfully gotten documents from a collection 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

In this tutorial, we have seen that MongoDB has a $exists operator to check whether a field exists in a document in MongoDB specified collection. Hope you enjoyed reading this tutorial.

Reference

https://www.mongodb.com/docs/manual/reference/operator/query/exists/#mongodb-query-op.-exists

Aneesha S
Aneesha S
Articles: 172