

Python MongoDB - find_one Query
source link: https://www.geeksforgeeks.org/python-mongodb-find_one-query/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:
- 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
- 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)
- 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 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:
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)
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)
Output:
{'name': 'Vishwash', 'Roll No': '1001'}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommend
-
13
Monitor MongoDB (query) load with Custom Metrics Robert Beekman on Jul 4, 2016 “I absolutely love AppSignal.” Discover AppSignal
-
4
MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in...
-
5
MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in...
-
10
MongoDB is a NoSQL database management system. Unlike MySQL the data in MongoDB is not stored as relations or tables. Data in mongoDB is stored as documents. Do...
-
4
Reading Time: 2 minutes Analyze query performance in mongodb may became complicated if we do not really know which part should be measured. Fortunately, MongoDB provides very handy tool which can be used to evaluate query performance: ...
-
6
Next →How to Get Started with MongoDB Atlas and Confluent Cloud Every year more and more applications are leveraging the public cloud and reaping the benefits of elastic scale and rapid provisio...
-
7
How to do Query Optimization in MongoDBHuge amounts of data are exchanged every day, and virtually everything has made its way on to the internet. So of course we need databases to save the data and retrieve it when needed. There are a...
-
6
Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...
-
10
Using LINQ to Query MongoDB in a .NET Core ApplicationApril 6, 2022
-
11
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK