I want to write a function to return all the documents contained in mycollection
in mongodb
from pymongo import MongoClient if __name__ == '__main__': client = MongoClient("localhost", 27017, maxPoolSize=50) db=client.mydatabase collection=db['mycollection'] cursor = collection.find({}) for document in cursor: print(document)
However, the function returns: Process finished with exit code 0
To find documents that match a set of selection criteria, call find() with the <criteria> parameter. MongoDB provides various query operators to specify the criteria.
The find_One() method of pymongo is used to retrieve a single document based on your query, in case of no matches this method returns nothing and if you doesn't use any query it returns the first document of the collection.
Here is the sample code which works fine when you run from command prompt.
from pymongo import MongoClient if __name__ == '__main__': client = MongoClient("localhost", 27017, maxPoolSize=50) db = client.localhost collection = db['chain'] cursor = collection.find({}) for document in cursor: print(document)
Please check the collection name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With