How to check if the PyMongo Cursor is Empty?


PyMongo is one of the libraries in python which provides a way to interact with the MongoDB , the most popular NoSQL document oriented database. It allows the developers to easily connect with the mongoDB instances and interact with the databases and collections, insert and retrieve the documents and other various operations.

Cursors in PyMongo

A cursor in MongoDB is an object that points to the documents. When we execute the find() method by passing a search query as a parameter, the results of the given query are returned in the form of a cursor object. By iterating through this cursor object, one can traverse through the documents in the obtained result.

In PyMongo library the cursor object is represented by the Cursor class. This object contains the results of the given search query.

We can provide search query can using find() function of the pymongo package, it’s returns value is in object format of type PyMongo. If we want to retrieve a single document, we can use the PyMongo.findone() function.

Verifying if a cursor is empty

The Cursor class provides an attribute cursor.alive which is used to check if the pymongo cursor is empty or not. The cursor.alive attribute returns the value “True” if there are more documents, else it returns “False” .

Installing PyMongo

First we have to install the PyMongo in our python envinornment. The below is the code.

pip install pymongo

Output

The following is the output of the above code after installing the pymongo.

Collecting pymongo
  Downloading pymongo-4.3.3-cp39-cp39-win_amd64.whl (382 kB)
     ------------------------------------ 382.5/382.5 kB 132.4 kB/s eta 0:00:00
Collecting dnspython<3.0.0,>=1.16.0
  Downloading dnspython-2.3.0-py3-none-any.whl (283 kB)
     ------------------------------------- 283.7/283.7 kB 21.2 kB/s eta 0:00:00
Installing collected packages: dnspython, pymongo
Successfully installed dnspython-2.3.0 pymongo-4.3.3
Note: you may need to restart the kernel to use updated packages.

Example

Following example demonstrates, how to check whether the pymongo cursor is empty or not using the cursor.alive attribute of the pymongo package.

import pymongo
client = pymongo.MongoClient("mongodb://localhost:7000/")
db = client["mydatabase"]
collection = db["mycollection"]
cursor = collection.find({})
if not cursor.alive:
    print("The Cursor is empty")
else:
    print("The Cursor is not empty")

Output

When we run the above code, following output will be generated -

The Cursor is not empty

Example

In the previous example we use find() function of the PyMongo package to check if the PyMongo cursor has multiple documents or empty. In the following example we are trying to check id the cursor returned by the find() function is empty.

import pymongo
client = pymongo.MongoClient("mongodb://localhost:7000/")
db = client["mydatabase"]
collection = db["mycollection"]
cursor = collection.findone({})
if not cursor.alive:
    print("The Cursor is empty")
else:
    print("The Cursor is not empty")

Output

When we run the above code, following output will be generated -

The Cursor is not empty

Note − We can use anyone of the functions either find() or find_one() as per our requirement and usage.

Updated on: 09-Aug-2023

739 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements