Wednesday, July 1, 2026
HomeLanguagesGet all the Documents of the Collection using PyMongo

Get all the Documents of the Collection using PyMongo

To get all the Documents of the Collection use find() method. The find() method takes a query object as a parameter if we want to find all documents then pass none in the find() method. 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. Note: If we pass no parameter in find() method .it works like select * in MYSQL . Sample Database: Let’s suppose the database looks like this Example 1: 

Python3




import pymongo
   
   
# establishing connection
# to the database
client = pymongo.MongoClient("mongodb://localhost:27017/")
   
# Database name
db = client["mydatabase"]
   
# Collection name
col = db["gfg"]
 
# if we don't want to print id then pass _id:0
for x in col.find({}, {"_id":0, "coursename": 1, "price": 1 }):
    print(x)


Output: Example 2: 

Python3




import pymongo
   
   
# establishing connection
# to the database
client = pymongo.MongoClient("mongodb://localhost:27017/")
   
# Database name
db = client["mydatabase"]
   
# Collection name
col = db["gfg"]
 
# if we don't want to print id then pass _id:0 and price :0
for x in col.find({}, {"coursename": 1}):
    print(x)


Output:

RELATED ARTICLES

Most Popular

Dominic
32517 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6966 POSTS0 COMMENTS