MongoDB : How to make query result look nice in mongo shell

Whenever we do find() query in mongoDB collection the shell is filled up with too much of data which is difficult to understand and ugly to look.

 

> db.devices.find()
{ "_id" : ObjectId("55ff79deb86a4b0eb1110ba4"), "time_allocated" : null,  "is_allocated" : false, "aggr_zone" : "xxx", "dc" : "DFW1", "time_suspended" : null, "device_type" : "server", "core_template_id" : "12345", "device_swapped_to" : null, "is_suspended" : false, "time_created" : "Sun Sep 20 22:30:38 2015", "device_id" : 48080, "is_decommed" : false }
{ "_id" : ObjectId("55ff79deb86a4b0eb1110ba5"), "time_allocated" : null,  "is_allocated" : false, "aggr_zone" : "xxx", "dc" : "ORD1", "time_suspended" : null, "device_type" : "server", "core_template_id" : "54321", "device_swapped_to" : null, "is_suspended" : false, "time_created" : "Sun Sep 20 22:30:38 2015", "device_id" : 45244, "is_decommed" : false }


 

To make the query result data look nicer there are 2 ways.
1. Use Pretty()

> db.devices.find().pretty()
{
	"_id" : ObjectId("55ff79deb86a4b0eb1110ba4"),
	"time_allocated" : null,
	"is_allocated" : false,
	"aggr_zone" : "xxx",
	"dc" : "DFW1",
	"time_suspended" : null,
	"device_type" : "server",
	"core_template_id" : "12345",
	"device_swapped_to" : null,
	"is_suspended" : false,
	"time_created" : "Sun Sep 20 22:30:38 2015",
	"device_id" : 48080,
	"is_decommed" : false
}
{
	"_id" : ObjectId("55ff79deb86a4b0eb1110ba5"),
	"time_allocated" : null,
	"is_allocated" : false,
	"aggr_zone" : "xxx",
	"dc" : "ORD1",
	"time_suspended" : null,
	"device_type" : "server",
	"core_template_id" : "54321",
	"device_swapped_to" : null,
	"is_suspended" : false,
	"time_created" : "Sun Sep 20 22:30:38 2015",
	"device_id" : 45244,
	"is_decommed" : false
}

Note : This would give your iterator, which mean you need to type "it" for next 20 records.
2. Use to Array

> db.devices.find().limit(2).toArray()
[
{
“_id” : ObjectId(“55ff79deb86a4b0eb1110ba4”),
“time_allocated” : null,
“is_allocated” : false,
“aggr_zone” : “xxx”,
“dc” : “DFW1”,
“time_suspended” : null,
“device_type” : “server”,
“core_template_id” : “12345”,
“device_swapped_to” : null,
“is_suspended” : false,
“time_created” : “Sun Sep 20 22:30:38 2015”,
“device_id” : 48080,
“is_decommed” : false
},
{
“_id” : ObjectId(“55ff79deb86a4b0eb1110ba5”),
“time_allocated” : null,
“is_allocated” : false,
“aggr_zone” : “xxx”,
“dc” : “ORD1”,
“time_suspended” : null,
“device_type” : “server”,
“core_template_id” : “54321”,
“device_swapped_to” : null,
“is_suspended” : false,
“time_created” : “Sun Sep 20 22:30:38 2015”,
“device_id” : 45244,
“is_decommed” : false
}
]
>

Note : This will display all records in shell, so use limit. Iterator is not given, so you might get any records mentioned unless you give sorting option.


		

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s