Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all documents of a collection using Pymongo

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

like image 903
MAYA Avatar asked Jun 21 '16 10:06

MAYA


People also ask

What is the Pymongo command to find all documents that match search criteria?

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.

What does find_One return Pymongo?

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.


1 Answers

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.

like image 126
notionquest Avatar answered Sep 21 '22 00:09

notionquest