Get all documents of a collection using Pymongo Get all documents of a collection using Pymongo python python

Get all documents of a collection using Pymongo


Here is the sample code which works fine when you run from command prompt.

from pymongo import MongoClientif __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.


pymongo creates a cursor. Hence you'll get the object 'under' the cursor. To get all objects in general try:

list(db.collection.find({}))

This will force the cursor to iterate over each object and put it in a list()

Have fun...


I think this will work fine in your program.

cursor = db.mycollection # choosing the collection you needfor document in cursor.find():    print (document)