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
client = pymongo.MongoClient("mongodb: / / localhost: 27017 / ")
db = client["mydatabase"]
col = db["gfg"]
for x in col.find({}, {"_id": 0 , "coursename": 1 , "price": 1 }):
print (x)
|
Output: Example 2:
Python3
import pymongo
client = pymongo.MongoClient("mongodb: / / localhost: 27017 / ")
db = client["mydatabase"]
col = db["gfg"]
for x in col.find({}, {"coursename": 1 }):
print (x)
|
Output:
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...