Menu Close

Python MongoDB Find Document

Python MongoDB Find Document

Hello Python developers, In this article, you will learn everything about Python MongoDB find Document. In the previous tutorial, we have seen all about How to insert documents into MongoDB collections using Python.

Note:- MongoDB find is the same as a SELECT statement in SQL databases.

Python MongoDB find document

PyMongo database driver provides two methods to find documents in MongoDB using Python.

Note:- In this article, we will not insert any documents into collections because this part we have done in our previous tutorial. Click here to know how to insert documents into MongoDB collections.

  • find_one():- Get only one document from MongoDB collection.
  • find():- Find all the documents from MongoDB collection.

Find One Document

The find_one() the method is used to select one document from the MongoDB collection.

Example:- Python MongoDB find one document


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

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]

# find one documents
result = collection.find_one()

print(result)

Output

{'_id': ObjectId('616a64648213bf456f68a3ad'), 'name': 'Vishvajit', 'Occupation': 'Software Developer', 'age': 24}

Get All Documents From MongoDB

To get all the documents from the MongoDB collection, use the find() method.

Example: Python MongoDB find all document


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

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]

# find one documents
all_documents = collection.find()

# use for loop to iterate

for document in all_documents:
    print(document)

When the above code will complete successfully, your output looks like something like this.

Output


{'_id': 1, 'name': 'John', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': 2, 'name': 'Peter', 'Occupation': 'DBA', 'age': 33}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 30}
{'_id': 4, 'name': 'Harry', 'Occupation': 'Python Dev', 'age': 35}

Find Specific Field in MongoDB

Sometimes you don’t want to get all the fields of the document, So at that time you can get only a specific field of the document.

The second parameter of the find() method is the dictionary of fields with the value set 1 you want to get. If the field value is set 0 that means, the field will not include in the result set. This parameter is optional.
By default values of all the fields are set 1.

Suppose we want to get only the names and ages of the employees.

Example:- Python MongoDB find document


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

# database
database = client["programmingfundadb"]

# collection
collection = database["employees"]

# find one documents
all_documents = collection.find({}, {"name": 1, "Occupation": 0, "age": 1})

# use for loop to iterate

for document in all_documents:
    print(document)

Output


{'name': 'John', 'age': 30}
{'name': 'Peter', "age": 33}
{'name': 'Jarsi', 'age': 30}
{'name': 'Harry', 'age': 35}

Conclusion

Here we have seen everything about Python MongoDB find document . Throughout this article, we have explored two ways to find documents from MongoDB collections. And these both the method are find() and find_one().

I strongly recommend you, If you need only one document then always use the find_one() and otherwise, you can go with the find() method.

If you find this article helpful, please share, support, and keep visiting for further Python MongoDB tutorials.

Reference:- Click Here

Thanks for reading ….

Previous Article
Next Article

Related Posts