572

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.

raffian
  • 31,267
  • 26
  • 103
  • 174

8 Answers8

949

(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()
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
437

You can add

DBQuery.prototype._prettyShell = true

to your file in $HOME/.mongorc.js to enable pretty print globally by default.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
staackuser2
  • 12,172
  • 4
  • 42
  • 40
  • 13
    You'll want to make a custom function that disables it for you. Add this to your `$HOME/.mongorc.js`: https://gist.github.com/mathrawka/6239405 Then you can do something like db.users.find().ugly() to get it without pretty print. – staackuser2 Aug 15 '13 at 09: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! – previous_developer Sep 21 '13 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 calling `db.collection.find().limit(1)` – Green Aug 23 '15 at 06:24
  • 1
    @staackuser2, that link seems to be broken :( – micseydel Jun 05 '18 at 00:34
  • This should be the accepted answer as the question is pointed to: how to make this a default option. The user is already familiar with the .pretty() method but does not want to write it every time – Ariel Monaco Jul 04 '18 at 11:59
  • 1
    Would be nice if this was set to true by default – Kubie Jul 23 '19 at 21:36
63

(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.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Bhanu Chawla
  • 1,138
  • 2
  • 13
  • 26
  • 1
    This worked for me perfectly. | MongoDB shell version: 2.6.7 and Oh-My-Zsh [version e55c715](https://github.com/robbyrussell/oh-my-zsh/tree/e55c715508a2f652fed741f2047c66dda2c6e5b0) – tjfwalker Feb 27 '15 at 00:23
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/

Aafreen Sheikh
  • 4,949
  • 6
  • 33
  • 43
  • 2
    Interestingly 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.. – kodybrown Aug 06 '15 at 18:14
19

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

db.collection.find().forEach(printjson);
Goff
  • 343
  • 3
  • 7
9

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.

Gaurav Gandhi
  • 3,041
  • 2
  • 27
  • 40
6

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)
Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67
-1

Check this out:

db.collection.find().pretty()
Mohammad Heydari
  • 3,933
  • 2
  • 26
  • 33