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 documents.
-
Now: how do you print it from external javascript loaded into Mongo?– Witold KaczurbaMay 13, 2019 at 6:46
-
@WitoldKaczurba Try searching for the answer to your question. If you don't find an answer, go ahead and post it as a new question.– MForMarlonJul 16, 2019 at 23:16
-
If you want colorized output and beautiful logging with MongoDB and Ruby on Rails, you can checkout github.com/ibraheemdev/mongo_beautiful_logger– Ibraheem AhmedAug 3, 2020 at 16:42
8 Answers
(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()
-
19If you want output colorized + other enhancements check out github.com/TylerBrock/mongo-hacker @SAFX Oct 10, 2012 at 22:42
-
Now: how do you print it from external javascript loaded into Mongo? This will not work... May 13, 2019 at 6:46
-
You can add
DBQuery.prototype._prettyShell = true
to your file in $HOME/.mongorc.js
to enable pretty print globally by default.
-
13You'll want to make a custom function that disables it for you. Add this to your
$HOME/.mongorc.js
: gist.github.com/mathrawka/6239405 Then you can do something like db.users.find().ugly() to get it without pretty print. Aug 15, 2013 at 9:01 -
5+1 This is great. For those who doesn't have a .mongorc.js file in $HOME folder; you can create this file and add commands in it. After you restart shell, it works! Sep 21, 2013 at 11:45
-
How can I verify in a cli or in mongo shell that this option has been enabled? I mean, is there a special command for it, like
mongo --checkConf 'pretty'
, not by callingdb.collection.find().limit(1)
– GreenAug 23, 2015 at 6:24 -
1
-
1
(note: this is answer to the updated question)
You can just do this on the CLI:
echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js
And it's always going to output pretty results.
-
1This worked for me perfectly. | MongoDB shell version: 2.6.7 and Oh-My-Zsh version e55c715 Feb 27, 2015 at 0:23
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/
-
2Interestingly enough, from the console the
.toArray()
function outputs better formatted JSON than the.pretty()
function. ie: The first record of my collection is different than the rest (which may be the problem), but.pretty()
dumps it out as{ "_id" : "VERSION", "v" : "1.5" }
- all on one line, where.toArray()
formatted it nice like the rest of the records.. Aug 6, 2015 at 18:14
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.
-
-
Note: when you install mongo-hacker using npm, it apparently overwrites
$HOME/.mongorc.js
.– HubroJun 9, 2016 at 11:50
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)