MongoDB is a cross-platform document-oriented database program and the most popular NoSQL database program. The term NoSQL means non-relational. MongoDB stores the data in the form of key-value pairs. It is an Open Source, Document Database which provides high performance and scalability along with data modeling and data management of huge sets of data in an enterprise application. MongoDB also provides the feature of Auto-Scaling. It uses JSON like documents, which makes the database very flexible and scalable.
Finding data from the collection or the database
In MongoDB, there are 2 functions that are used to find the data from the collection or the database.
- find_one()
- find()
Find_one()
In MongoDB, to select data from the collection we use find_one() method. It returns the first occurred information in the selection and brings backs as an output. In find_one() method there are no parameters required as it brings the first occurrence of information from the database.
Example 1: Find the first document from the student’s collection/database.
Let’s suppose the database looks like this –

# Python program to demonstrate # find_one() import pymongo mystudent = pymongo.MongoClient('localhost', 27017) # Name of the databse mydb = mystudent["gfg"] # Name of the collection mycol = mydb["names"] x = mycol.find_one() print(x) |
Output :
![]()
Find()
find() method is used to select data from the database. It returns all the occurrences of the information stored in the collection. It has 2 types of parameters, The first parameter of the find() method is a query object. In the below example we will use an empty Query object, which will select all information from the collection.
Note: It works the same as SELECT* without any parameter.
Example:
import pymongo # establishing connection # to the database my_client = pymongo.MongoClient('localhost', 27017) # Name of the databse mydb = my_client["gfg"] # Name of the collection mynew = mydb["names"] for x in mycol.find(): print(x) |
Output :

The second parameter to the find() method is that you can specify the field to include in the result. The second parameter passed in the find() method is of object type describing the field. Thus, this parameter is optional.
If omitted then all the fields from the collection/database will be displayed into the result.
To include the field in the result the value of the parameter passed should be 1, if the value is 0 then it will be excluded from the result.
Example: Return only the names and address, not the id:
import pymongo # establishing connection # to the database my_client = pymongo.MongoClient('localhost', 27017) # Name of the databse mydb = my_client["gfg"] # Name of the collection mynew = mydb["names"] for x in mycol.find({}, {"_id":0, "name": 1, "address": 1 }): print(x) |
Output:

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.
Recommended Posts:
- MongoDB and Python
- MongoDB Python | Insert and Update Data
- MongoDB python | insert(), replace_one(), replace_many()
- MongoDB python | Delete Data and Drop Collection
- Python MongoDB - Sort
- Create a database in MongoDB using Python
- 3D Plotting sample Data from MongoDB Atlas Using Python
- Geospatial Queries with Python MongoDB
- Python Mongodb - Delete_one()
- Python Mongodb - Delete_many()
- Python MongoDB - Update_one()
- How to fetch data from MongoDB using Python?
- How to access a collection in MongoDB using Python?
- Python MongoDB - insert_many Query
- Drop Collection if already exists in MongoDB using Python
- Python MongoDB - Update_many Query
- Python MongoDB - insert_one Query
- Python MongoDB - find_one Query
- How to create index for MongoDB Collection using Python?
- Python MongoDB - create_index Query
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.

