Quantcast
Channel: Ramblings on life & code » James Tan
Viewing all articles
Browse latest Browse all 10

MongoDB Titbits #1

$
0
0

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:

MongoDB shell version: 2.8.0-rc1
connecting to: test
> EDITOR='vim'
vim

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:
> edit myquery

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

function myquery() {
    print("Hello world")
    db.test.insert({ a: 1 })
    printjson(db.test.findOne())
}

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

> myquery()
Hello world
{ "_id" : ObjectId("547984f1f9c9174388161a3e"), "a" : 1 }

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:

^[[35;1mundefined^[[m

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:

$ m 2.8.0-rc1

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

MongoDB version 2.8.0-rc1 is not installed.
Installation may take a while. Would you like to proceed? [y/n]
... installing binary
######################################################### 100%
... removing source
... installation complete

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

$ m
    2.6.4
    2.6.5
  ο 2.8.0-rc1

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:

#!/bin/bash
# Convert syslog MongoDB log files to MongoDB format, to prevent mtools
# (mloginfo, etc.) from choking.

F=$1
awk '{$4=$5=""; print "Mon " $0}' $F > $F.mongod

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:
$ syslog2mongod.sh mongod.log


Viewing all articles
Browse latest Browse all 10

Trending Articles