Tip #1: mongo-hacker for Mongo Shell

If you spend a lot of time using the Mongo Shell like I do, you’ll love mongo-hacker. It’s a set of Mongo Shell enhancements that literally adds colour to your otherwise dull Mongo Shell life. For example, here’s a regular Mongo Shell session:
mongo-hacker-before-1 Once mongo-hacker is installed, the same session looks like this:
mongo-hacker-after-1 Much more readable with syntax highlighting isn’t it? In particular, I really like how it includes the current database in the prompt – very handy as I’ve seen a lot of people getting confused simply because they were in the wrong database (e.g. could have been the default “test” database or an unnoticed typo when switching). It also pretty prints all results by default, making it much easier to read query results.

Tip #2: Your favourite editor in Mongo Shell

Typing or debugging a long query or aggregation command can be tricky if all done in a single line of the Mongo Shell. There are several ways around that, but a handy way is to use an external editor like vim or emacs. First you’ll need to define the EDITOR variable like so:

You can add this to ~/.mongorc.js  to avoid typing it each time. Now you can use the edit  command to bring up the specified external editor. For example:

This brings up vim with a single line “undefined”. Replace that with a Javascript function to do whatever you want. For example:

Exit the editor to return to Mongo Shell. Now you can run the function you’ve just declared:

Note that writing commands in Javascript like this is slightly different from the interactive Mongo Shell. For details, refer to the docs for writing scripts and Mongo Shell.

Issue with mongo-hacker

If you’re using mongo-hacker, you’ll see garbled text in the editor like the following due to this bug:

Stennie’s branch fixes this but it’s not yet in master at this time of writing. For your convenience, save this file to ~/.mongorc.js  and restart Mongo Shell.

Tip #3: RoboMongo

I’m a terminal/console person so I rarely use GUI tools, but they sometimes come in handy. These are many MongoDB admin GUIs out there, but my favourite and thus the one I recommend most is RoboMongo. It’s cross platform (Mac, Windows, and Linux), lets you click around to explore the database and yet retains the Mongo Shell flexibility combined with auto-completion:

RoboMongo

MongoVue has a bit more features, but only works on Windows. The relatively new MongoChef lets you drag and drop fields to construct complex queries and aggregations.

Tip #4: Managing multiple MongoDB versions

It’s often useful to have multiple versions of MongoDB installed locally and a way to quickly switch between them for testing. I was about to write one myself when I discovered m, which does exactly this. For example, to use a specific version of MongoDB, simply run:

If it’s already installed, it switches over instantly. Otherwise you’ll need to confirm the installation:

To see the list of installed MongoDB versions (active one is marked):

Huge time saver and no more excuses not to run or test the latest releases!

Tip #5: mtools (with syslog)

mtools is a collection of MongoDB utilities that are extremely helpful for performance tuning and diagnostics. Too much to describe here, but check out this introductory blog post and related presentation.

What I do want to mention is this small hack to workaround the inability to parse MongoDB logs that are sent through syslog:

This script can be saved to syslog2mongod.sh  and executed like the following to convert syslog MongoDB files to regular MongoDB log file format the mtools can parse: