Python MongoDB – find_one Query

Prerequisites: MongoDB Python Basics

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

Let’s begin with the find_one() method:

  1. Importing PyMongo Module: Import the PyMongo module using the command:
    from pymongo import MongoClient

    If MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with Python

  2. Creating a Connection: Now we had already imported the module, its 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)
  3. 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
  4. Accessing the Collection: We now select the collection from the database using the following syntax:
    collection_name = mydatabase.name_of_collection
  5. Finding in the collection: Now we will find in the database using find_one() function. This function return only one value if the data is found in the database else it returns None. It is ideal for those situations where we need to search for the only one document.

    Syntax:

    find_one(filter=None, *args, **kwargs)

Example 1:



Sample Database:

python-mongodb-insert-one-21

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.