Pretty print in MongoDB shell as default

前端 未结 8 1752
予麋鹿
予麋鹿 2020-12-07 06:50

Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it\'s difficult to read, especially with nested arrays and document

相关标签:
8条回答
  • 2020-12-07 07:04

    Since it is basically a javascript shell, you can also use toArray():

    db.collection.find().toArray()
    

    However, this will print all the documents of the collection unlike pretty() that will allow you to iterate. Refer: http://docs.mongodb.org/manual/reference/method/cursor.toArray/

    0 讨论(0)
  • 2020-12-07 07:08

    Give a try to Mongo-hacker(node module), it alway prints pretty. https://github.com/TylerBrock/mongo-hacker

    More it enhances mongo shell (supports only ver>2.4, current ver is 3.0), like

    • Colorization
    • Additional shell commands (count documents/count docs/etc)
    • API Additions (db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), etc)
    • Aggregation Framework

    I am using for while in production env, no problems yet.

    0 讨论(0)
  • 2020-12-07 07:10

    (note: this is answer to original version of the question, which did not have requirements for "default")

    You can ask it to be pretty.

    db.collection.find().pretty()
    
    0 讨论(0)
  • 2020-12-07 07:11

    Oh so i guess .pretty() is equal to:

    db.collection.find().forEach(printjson);
    
    0 讨论(0)
  • 2020-12-07 07:13

    Got to the question but could not figure out how to print it from externally-loaded mongo. So:

    This works is for console: and is prefered in console, but does not work in external mongo-loaded javascript:

    db.quizes.find().pretty()
    

    This works in external mongo-loaded javscript:

    db.quizes.find().forEach(printjson)
    
    0 讨论(0)
  • 2020-12-07 07:16

    Check this out:

    db.collection.find().pretty()
    
    0 讨论(0)
提交回复
热议问题