In this MongoDB tutorial, we are going to learn “How to check if the document exists In MongoDB” with a few examples. We will cover this by implementing various operations with examples. The following list topics we are going to cover in this tutorial:
- MongoDB check if the document exists query
- MongoDB check if the document exists in a collection
- MongoDB check if the document exists before insert
- MongoDB check if the document already exists
- MongoDB check if the document exists using python
MongoDB check if the document exists query
In MongoDB, you can check the existence of the field in the defined collection using the $exists, operator. Here, if the value of the $exists operator is true then the operator matches the document that holds the specified field( including the documents where the value of that field is null).
When we set the value of $exists operator is false, then this operator returns only those documents that don’t contain the specified field.
Syntax:
{ field: { $exists: <boolean> } }
Example:
The following documents were inserted into the example collection.
db.example.find()
{ "_id" : "101", "name" : "James", "age" : "26", "city" : "USA" }
{ "_id" : "102", "name" : "Noah", "age" : "22" }
{ "_id" : "103", "name" : "Robert", "age" : "28", "city" : "United States" }
{ "_id" : "104", "name" : "Peter", "age" : "23", "city" : "Canada" }
{ "_id" : "105", "name" : "Jack", "age" : "25" }
Now, we will apply the below query to check the existence of the field in the example collection:
db.example.find( { city: { $exists: true} } )
Here, we have used the $exists operator set true to check the field exists or not in the example collection.
Output:

Also, check: MongoDB find by ID
MongoDB check if the document exists in a collection
In this topic, you will understand how to check if the document exists in a collection or not with the help of an example. And, we will check the existence of the field of the embedded document.
Let’s get understand this with the help of an example and find documents if exists in the collection.
Example:
The following documents were inserted into the inventory collection.
db.inventory.find()
{ "_id" : 1, "dept" : "A", "item" : { "sku" : "101", "color" : "red" }, "sizes" : [ "S", "M" ] }
{ "_id" : 2, "dept" : "A", "item" : { "sku" : "102", "color" : "blue" }, "sizes" : [ "M", "L" ] }
{ "_id" : 3, "dept" : "B", "item" : { "sku" : "103", "color" : "blue" }, "sizes" : "S" }
{ "_id" : 4, "dept" : "A", "item" : { "sku" : "104", "color" : "black" }, "sizes" : [ "S" ] }
After that, we will apply the below query to check the existence of the field of the embedded document.
db.inventory.find( { "item.color": { $exists: false } } )
Here, we have used the $exists operator set false to display all the documents fields where the embedded document does not exist.
Output:

Read: Distinct query in MongoDB
MongoDB check if a document exists before insert
In this topic, we will check if a document exists or not in the MongoDB collection before inserting any document into the collection. Let’s get understand this topic with the help of an example and check if a document exists before inserting any document into the collection.
Example:
In this example, we are using the info collection and the following list of collections are inside the info database.
# To select the database
use info
# To check the collection
show collections

Now, we will apply the below query to check if a document exists before inserting any document in the collection.
db.books.find( { "_id": 1 } ).count() > 0
Here, we have defined the count() method that will count the documents where _id is 1 and the greater than 0 then display true means document exists in the collection or display false means document don’t exist in the collection.
You can check the output, it will display false because there is no “books” collection existing in the “info” database.
Output:

Read: MongoDB compare two fields
MongoDB check if the document already exists
In this topic, we will check if the document already exists or not in the MongoDB collection. Here, if the document already exists then it will return true, or if not exist then return false.
Example:
The following documents were inserted into the details collection.
db.details.find()
{ "_id" : 1, "item" : "journal", "qty" : 15, "tags" : [ "appliances", "school", "clothing" ] }
{ "_id" : 2, "item" : "notebook", "qty" : 20, "tags" : [ "school" ] }
{ "_id" : 3, "item" : "paper", "qty" : 25, "tags" : [ "appliances", "school" ] }
{ "_id" : 4, "item" : "planner", "qty" : 30, "tags" : [ "school", "appliances" ] }
{ "_id" : 5, "item" : "postcard", "qty" : 20, "tags" : [ [ "appliances", "school" ], "clothing" ] }
After that, we will apply the subsequent query to check the document is exists or not.
Case 1: The following is the query will return true if a document exists
db.details.find( { "_id": 5}).count() > 0
Here, we have defined the count() method that will count the documents where _id is 5 and the greater than 0 then display true means document exists in the collection.
Output:

Case 2: The following is the query that returns false if a document does not exist
db.details.find( { "_id": 7 } ).count() > 0
Here, we have defined the count() method that will count the documents where _id is 7 and the greater than 0 then display false means document exists in the collection.
Output:

Read: How to change collection name in MongoDB
MongoDB check if the document exists using python
In this topic, you will understand to check if the document exists or not in the MongoDB collection using python. Here, we will use the MongoDB driver PyMongo to access the MongoDB database. Let’s get understand this with the help of an example and check if the document exists or not in the collection using python.
Example:
The following code will use to check if the document exists in the collection.
import pymongo
# creating a MongoClient object and connect with the host
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
# accessing the database
mydb = myclient["info"]
# accessing the collection of the database
mycol = mydb["example"]
myquery =[{ "_id": 1, "name": "Jack", "address": "Valley 345, Australia" },
{ "_id": 2, "name": "Bob", "address": "sallong 541, USA" },
{ "_id": 3, "name": "Maze", "address": "Trick 302, Canada" }]
mycol.insert_many(myquery)
# print true if collection exists
exists = mycol.count_documents({ "_id": 2 }) > 0
print(exists)
In the code, we import the pymongo library to access the MongoDB database.
- After that, we have created a MongoClient object and connect with the host.
- Next, Access the database “info”.
- We have inserted some documents into the “example” collection.
- After that, we have used the count_documents() function in pymongo to count the number of documents present in the collection.
- Next, if count_documents({ “_id”: 2 }) greater than 0 then it will display true means document exist in the collection else false.
Output:

You may also like to read the following tutorials on MongoDB.
- MongoDB find last inserted document
- MongoDB return only matching array elements
- How to get id from mongodb after insert
- MongoDB date format – Complete Tutorial
- MongoDB order by date – Detailed Guide
- MongoDB find string contains
- MongoDB return specific fields
- How to delete documents in MongoDB
I hope you have learned about check if the document exists in MongoDB with a few examples and cover the below topics:
- MongoDB check if a document exists query
- MongoDB check if the document exists in a collection
- MongoDB check if a document exists before insert
- MongoDB check if the document already exists
- MongoDB check if the document exists using python
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.