db.col.find().pretty() not working

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.


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


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!

链接地址: http://www.djcxy.com/p/50236.html

上一篇: MongoDB打印漂亮与PyMongo

下一篇: db.col.find()。pretty()不起作用