Tip: List non replication operations when using db.currentOp() in the MongoDb shell

We run ad-hoc queries against our MongoDB hidden slaves pretty frequently at work. Some of these queries are long running so it nice to be get a filtered view of what operations are running without all the replication operations that are constantly running.

The mongo shell is a javascript interpreter so that is easy to do:

db.currentOp()["inprog"].filter(function(x) { return !x.desc.match(/repl.*/); });

Tip: A shortcut for accessing another MongoDB database from the shell

At uSwitch, I often work with multiple MongoDB databases as part of the same system. Recently I spent a lot of time in the Mongo shell jumping between the two databases to reset data to its initial state.

This requires several commands

use otherdb;
db.collectionx.remove();

use originaldb;
db.collectiony.remove();

Plus I often forget which database I’m in.

Happily you can use the db.getSiblingDB method to access another database without having to switch databases. This shorten it down to a single command:

db.getSiblingDB("otherdb").collectionx.remove();
db.getSiblingDB("originaldb").collectiony.remove();