how to fetch all the documents of the mongodb collection in python
Question: I want to fetch all the documents of the some collection of MongoDB and print
certain attributes within each document. But I am unable to do so.

My code:

    
    
    import csv
    
    from pymongo import MongoClient
    
    db_client = MongoClient()
    
    db = db_client.pg_new_sw_cur
    
    db_collection_users = db.pg_new_sw
    
    
    #with open("pg_details_new.csv",'w') as f:
    
    for post in db_collection_users.find():
    
            print post['user_name']
            print "==========================="
    

It is printing nothing on the screen after running it when the collection
consists of thousands of objects/documents when viewed using robomongo.

I am new to MongoDB. So, please help me.


Answer: It would be a wild guess, but I suspect that you have specified an incorrect
collection name:

    
    
    db_collection_users = db.pg_new_sw
    

Recheck that `pg_new_sw` actually exists in your local `MongoDB` server
instance and contains documents inside.

