Pricing Blog Sign In

Prettier MongoDB output

Profile image of Michael Lee
Written by Michael Lee
@michaelsoolee

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.