Wednesday, 7 August 2013

MongoDB - Make the find output pretty

By default, the mongo shell client returns a Bson which could be very hard to read. To solve this, there are two options: The static one and the dynatic one.

The dynamic solution
For the mongo client, there is a method called pretty(). You can called after any find method. For example:

db.collection.find().pretty()

The static solution
The static solution requires you to edit the mongo configuration file. Once it is done, when you call find method, it will print the human readable Bson instead of the normal Bson. To do so:

1. Go to your home directory. For Mac OS and Linux, just type the follow command in the terminal:

cd

2. Locate the .mongorc.js file in the home directory. It starts with a dot (.), which means it is a hidden file for unix based system (Linux and Mac OS). To see it, use the option "-a" of ls

ls -la

3. Open a file with an editor. For example, nano

nano .mongorc.js

4. Add the follow line in the file:

DBQuery.prototype._prettyShell = true

5. Save the file.

Source

No comments:

Post a Comment