Prettier MongoDB output

Written by Michael Lee on March 3, 2017

The database that TinyWins runs on is MongoDB. Once, while I was working on a new feature, a friend of mine who isn’t a programmer walked past my screen as I was running a query. She asked, “Why does it look so ugly and unreadable, can’t it be like prettier?”. She was referring to the output on my screen that looked something like this:

{ "_id" : "ryQtnzH9g", "updatedAt" : ISODate("2017-03-02T04:56:37.708Z"), "createdAt" : ISODate("2017-03-02T04:15:55.305Z"), "title" : "Testing new task", "userId" : "H1KrJmb9g", "completedAt" : null, "status" : "i", "subTasksShowing" : true, "subTaskIds" : [ ] }
{ "_id" : "ryiu6zS5e", "updatedAt" : ISODate("2017-03-02T04:40:01.485Z"), "createdAt" : ISODate("2017-03-02T04:20:03.417Z"), "title" : "Test another new one", "userId" : "H1KrJmb9g", "completedAt" : ISODate("2017-03-02T04:40:01.467Z"), "status" : "c", "subTasksShowing" : true, "subTaskIds" : [ ] }

The query I ran for the output above was:

db.tasks.find()

Thinking about her comment, I remembered that MongoDB has a function that will “prettify” its output by appending .pretty() to the end of a query. The .pretty()’s output looks a lot more human readable like this:

{
	"_id" : "ryQtnzH9g",
	"updatedAt" : ISODate("2017-03-02T04:56:37.708Z"),
	"createdAt" : ISODate("2017-03-02T04:15:55.305Z"),
	"title" : "Testing new task",
	"userId" : "H1KrJmb9g",
	"completedAt" : null,
	"status" : "i",
	"subTasksShowing" : true,
	"subTaskIds" : [ ]
}
{
	"_id" : "ryiu6zS5e",
	"updatedAt" : ISODate("2017-03-02T04:40:01.485Z"),
	"createdAt" : ISODate("2017-03-02T04:20:03.417Z"),
	"title" : "Test another new one",
	"userId" : "H1KrJmb9g",
	"completedAt" : ISODate("2017-03-02T04:40:01.467Z"),
	"status" : "c",
	"subTasksShowing" : true,
	"subTaskIds" : [ ]
}

The query ran for this output was:

db.tasks.find().pretty()

This output is definitely much prettier and easier to read, even for me.

This article was originally published at TinyWins.

Thanks for taking the time to read this article. I'd love to stay in touch and share more tips on programming and design, side projects and working remotely. Sign up and I'll send you my articles straight to your email.

:wave: Hey hey, I'm Michael Lee! I really appreciate you taking the time to read this article.

Be sure to check out my other articles.