Open In App

Python MongoDB – find_one Query

Last Updated : 29 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

This article focus on the find_one() method of the PyMongo library. find_one() is used to find the data from MongoDB. 

Prerequisites: MongoDB Python Basics 

Let’s begin with the find_one() method:

Importing PyMongo Module: Import the PyMongo module using the command:

from pymongo import MongoClient
  1. If MongoDB is already not installed on your machine, one can refer to the guide og how to Install MongoDB with Python.
  2. Creating a Connection: Now we had already imported the module, it’s time to establish a connection to the MongoDB server, presumably which is running on localhost (host name) at port 27017 (port number).
client = MongoClient(‘localhost’, 27017)

Accessing the Database: Since the connection to the MongoDB server is established. We can now create or use the existing database.

mydatabase = client.name_of_the_database

Accessing the Collection: We now select the collection from the database using the following syntax:

collection_name = mydatabase.name_of_collection

Finding in the collection: Now we will find in the collection using find_one() function. This function return only one document if the data is found in the collection else it returns None. It is ideal for those situations where we need to search for only one document. Syntax:

find_one(filter=None, *args, **kwargs)
python-mongodb-insert-one-21

Example 1: Basic Usage of find_one()

Let’s look at a basic example where we search for a document in a collection named Student within the GFG database. We will search for a document where the Branch field is equal to ‘CSE’.

# Python program to demonstrate
# find_one() method

# Importing Library
from pymongo import MongoClient


# Connecting to MongoDB server
# client = MongoClient('host_name','port_number')
client = MongoClient('localhost', 27017)


# Connecting to the database named
# GFG
mydatabase = client.GFG


# Accessing the collection named
# gfg_collection
mycollection = mydatabase.Student


# Searching through the database
# using find_one method.
result = mycollection.find_one({'Branch': 'CSE'})
print(result)

Output:

{'_id': 1, 'name': 'Vishwash', 'Roll No': '1001', 'Branch': 'CSE'}

Example 2: 

Here in this code we will use multiple filter, The filter condition {'Branch': 'CSE'} ensures that the query searches for a document where the Branch field is equal to 'CSE'. The projection {'_id': 0, 'name': 1, 'Roll No': 1} ensures that only the name and Roll No fields are included in the output, excluding the _id field. The result of this query is printed to the console

# Python program to demonstrate
# find_one() method

# Importing Library
from pymongo import MongoClient


# Connecting to MongoDB server
# client = MongoClient('host_name','port_number')
client = MongoClient('localhost', 27017)


# Connecting to the database named
# GFG
mydatabase = client.GFG


# Accessing the collection named
# gfg_collection
mycollection = mydatabase.Student


# Searching through the database
# using find_one method.
result = mycollection.find_one({'Branch': 'CSE'},
                               {'_id': 0, 'name': 1, 'Roll No': 1})
print(result)

Output:

{'name': 'Vishwash', 'Roll No': '1001'}

Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg