Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I enable mongodb cli pretty print? - db.col.find().pretty() not working

Tags:

mongodb

Using mongo v2.4.5 shell, db.col.find().pretty() does not pretty print for me on either osx console or linux ubuntu 12.04 bash.

There is no diff in the output with and without pretty()

> db.people.find()
{ "_id" : ObjectId("520d293752cfe6ece5d3fd77"), "name" : "Andrew" }
{ "_id" : ObjectId("520e448b77803f8f15fcfedb"), "name" : "Amy" }
> 
> db.people.find().pretty()
{ "_id" : ObjectId("520d293752cfe6ece5d3fd77"), "name" : "Andrew" }
{ "_id" : ObjectId("520e448b77803f8f15fcfedb"), "name" : "Amy" }
> 

What am I missing? (something crazy basic no doubt)

Thx


UPDATE: doh! answered below. I had not realized such a simple doc would not be prettified. Nested docs pretty fine for me.

like image 778
navicore Avatar asked Dec 09 '22 13:12

navicore


2 Answers

.pretty will only really change things when you have nested or larger documents:

> db.so.insert( { name: "Derick" } );
> db.so.insert( { f: 'Derick', s: 'Rethans', t: 'derickr' } );
> db.so.insert( { name: { f: 'Derick', s: 'Rethans' } } );

> db.so.find();
{ "_id" : ObjectId("520e49a21d7b77441eaf6446"), "name" : "Derick" }
{ "_id" : ObjectId("520e49b11d7b77441eaf6447"), "name" : { "f" : "Derick", "s" : "Rethans" } }

> db.so.find().pretty();
{ "_id" : ObjectId("520e49a21d7b77441eaf6446"), "name" : "Derick" }
{
    "_id" : ObjectId("520e4f895a4563e39f06b030"),
    "f" : "Derick",
    "s" : "Rethans",
    "t" : "derickr"
}
{
    "_id" : ObjectId("520e49b11d7b77441eaf6447"),
    "name" : {
        "f" : "Derick",
        "s" : "Rethans"
    }
}

So I presume it works just fine for you!

like image 84
Derick Avatar answered Mar 23 '23 01:03

Derick


You can add these lines to your file in $HOME/.mongorc.js in order to enable pretty print.

DBQuery.prototype._prettyShell = true

Alternatively, you can use this command that prints docs in array format:

db.collection.find().toArray()

cheers!

like image 20
Manish Kumar Avatar answered Mar 23 '23 01:03

Manish Kumar