raffian
raffian

Reputation: 32086

Pretty print in MongoDB shell as default

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.

Upvotes: 577

Views: 171036

Answers (8)

Witold Kaczurba
Witold Kaczurba

Reputation: 10505

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)

Upvotes: 7

Mohammad Heydari
Mohammad Heydari

Reputation: 4290

Check this out:

db.collection.find().pretty()

Upvotes: -1

Bhanu Chawla
Bhanu Chawla

Reputation: 1148

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

Upvotes: 63

Gaurav Gandhi
Gaurav Gandhi

Reputation: 3201

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.

Upvotes: 9

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230531

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

Upvotes: 957

Goff
Goff

Reputation: 343

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

db.collection.find().forEach(printjson);

Upvotes: 19

Aafreen Sheikh
Aafreen Sheikh

Reputation: 5074

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/

Upvotes: 23

staackuser2
staackuser2

Reputation: 12412

You can add

DBQuery.prototype._prettyShell = true

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

Upvotes: 439

Related Questions