Pretty printing of output in pymongo

前端 未结 4 1719
太阳男子
太阳男子 2021-01-12 17:25

I am using pymongo driver to work with Mongodb using Python. Every time when I run a query in python shell, it returns me some output which is very difficul

相关标签:
4条回答
  • 2021-01-12 17:58

    It probably depends on your IDE, not the pymongo itself. the pymongo is responsible for manipulating data and communicating with the mongodb. I am using Visual Studio with PTVS and I have such options provided from the Visual Studio. The PyCharm is also a good option for IDE that will allow you to watch your code variables and the JSON in a formatted structure.

    0 讨论(0)
  • 2021-01-12 18:00

    Actually you can also program it by yourself like:

    db = connection.[dbname]
    
    collection = db.[yourcollectionname]
    
    for col in collection.find({}):
    
        for keys in col.keys(): 
    
            print ('{', keys, ":" , col[keys] , '}' )
    

    I think this will be helpful or take it as an option.

    0 讨论(0)
  • 2021-01-12 18:19

    I want to know whether there is any method like pretty() in PyMongo

    No PyMongo doesn't provide such method. It is only available in the shell. You need to use the pprint function from the pprint module.

    0 讨论(0)
  • 2021-01-12 18:21

    No direct method to print out of pymongo in a structured way.

    as the out of pymongo is dict

    print(json.dumps('variable with out of pymongo query'))

    this will serve your purpose i think

    0 讨论(0)
提交回复
热议问题