Menu Close

How to Filter Records in MongoDB using Python ?

Filter Records in MongoDB using Python

Hello Python Programmers, In this article, we are going to see how to filter records in MongoDB using Python PyMongo.In the previous Python MongoDB tutorial, we have seen everything about how to find all the documents from MongoDB collections. But sometimes you want to select some specific records whose full fill the conditions.

In this guide, we will give you the complete guide to filter records in MongoDB using Python.

Python MongoDB Query

So far we have seen how to find all the documents from MongoDB collections, but in this article, we will filter the records or documents from specific conditions. To filter data in MongoDB, we will use the find() method. The find() method takes query object that limits the result set.

Example:- Filter Records in MongoDB using Python


import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]


# query object
query = {"age": 30}

# find one documents
all_documents = collection.find(query)

# use for loop to iterate
for document in all_documents:
    print(document)

Output


{'_id': ObjectId('616a66f217ca23e3953eac7b'), 'name': 'John', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': ObjectId('616a83fed28431f7a80bf995'), 'name': 'Alisa', 'Occupation': 'DBA', 'age': 30}
{'_id': 1, 'name': 'John', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 30}

Advanced Filter

To make a more advanced filter query you have to modify the query object.

Greater Than

Fetch all the employees were age greater than 25.

Example: Filter Records in MongoDB using Python


import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]


# query object
query = {"age": {"$gt": 25}}

# find one documents
all_documents = collection.find(query)

# use for loop to iterate
for document in all_documents:
    print(document)

Output


{'_id': ObjectId('616a66f217ca23e3953eac7b'), 'name': 'John', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': ObjectId('616a83fed28431f7a80bf993'), 'name': 'Peter', 'Occupation': 'Data Scientist', 'age': 33}
{'_id': ObjectId('616a83fed28431f7a80bf994'), 'name': 'Alexa', 'Occupation': 'Machine Learning Enginner', 'age': 35}
{'_id': ObjectId('616a83fed28431f7a80bf995'), 'name': 'Alisa', 'Occupation': 'DBA', 'age': 30}
{'_id': ObjectId('616a83fed28431f7a80bf996'), 'name': 'Harry', 'Occupation': 'Python Developer', 'age': 36}

Between Search

I am using between search filter to select documents between a specific range.

Example: MongoDB filter Document Fields

Get all the employees whose age is greater than 25 but less than 30.


import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]


# query object
query = {"age": {"$gt": 25, "$lt": 35}}

# find one documents
all_documents = collection.find(query)

# use for loop to iterate
for document in all_documents:
    print(document)

Output


{'_id': 1, 'name': 'John', 'Occupation': 'Software Enginner', 'age': 26}
{'_id': 2, 'name': 'Peter', 'Occupation': 'DBA', 'age': 29}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 27}

Greater Than or Equal

Find all the employees whose age is greater than or equal to 35.

Example: Python MongoDB Query


import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]


# query object
query = {"age": {"$gte": 35}}

# find one documents
all_documents = collection.find(query)

# use for loop to iterate
for document in all_documents:
    print(document)

Output


{'_id': ObjectId('616a83fed28431f7a80bf994'), 'name': 'Alexa', 'Occupation': 'Machine Learning Enginner', 'age': 35}
{'_id': ObjectId('616a83fed28431f7a80bf996'), 'name': 'Harry', 'Occupation': 'Python Developer', 'age': 36}
{'_id': 4, 'name': 'Harry', 'Occupation': 'Python Dev', 'age': 35}

Regular Exapression

You can also apply regular expression in your query object to filter the documents in MongoDB.

Note:- Regular can be only applied to string-related fields.

Example: How to Filter Records in MongoDB using Python?

Find all the documents where Occupation fields start with only M.


import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]


# query object
query = {"Occupation": {"$regex": "^M"}}

# find one documents
all_documents = collection.find(query)

# use for loop to iterate
for document in all_documents:
    print(document)

Output


{'_id': ObjectId('616a83fed28431f7a80bf994'), 'name': 'Alexa', 'Occupation': 'Machine Learning Enginner', 'age': 35}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 30}

Example: Python MongoDB Query

Find all the documents where Occupation fields end with only r.


import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]


# query object
query = {"Occupation": {"$regex": "r$"}}

# find one documents
all_documents = collection.find(query)

# use for loop to iterate
for document in all_documents:
    print(document)

Output


{'_id': ObjectId('616a64648213bf456f68a3ad'), 'name': 'Vishvajit', 'Occupation': 'Software Developer', 'age': 24}
{'_id': ObjectId('616a66f217ca23e3953eac7b'), 'name': 'John', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': ObjectId('616a83fed28431f7a80bf994'), 'name': 'Alexa', 'Occupation': 'Machine Learning Enginner', 'age': 35}
{'_id': ObjectId('616a83fed28431f7a80bf996'), 'name': 'Harry', 'Occupation': 'Python Developer', 'age': 36}
{'_id': 1, 'name': 'John', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 30}

Conclusion

So, In this article, we have seen all about filter records in MongoDB using Python. If you are using MongoDB in any Python applications then you will need to filter documents in MongoDB according to your requirements.

I hope this article will help you. If you like this Python MongoDB tutorial, please share, support, and keep visiting for further Python MongoDB tutorials.

Reference:- Click Here

Thanks for reading …

Python MongoDB Find Document
Python MongoDB Delete

Related Posts