MongoDB 2.6 Shell & Tools: Things Worth Knowing


Earlier this year, as part of a series on the Mongo Shell, we introduced readers to the .mongorc.js file. This file, which lives in a user's home directory, is automatically executed when the mongo shell is run, making it a great place to customise your Mongo shell experience. You can, for example, load it with time-saving functions and pre-initialised variables. In MongoDB 2.6 there's now another startup file, /etc/mongorc.js, which is a global shell startup file that is run before the user's own .mongorc.js file. If a number of users share various tools in the shell, it's really useful to have them in /etc/mongorc.js, as long as everyone has read permission for that file of course.

One thing you can't do is stop the global file from being run; the user's own startup file can be disabled with --norc as an option to mongo, but that doesn't affect the global file. There's two things this means. First, the global file is also a good place to put commands you want people to use and not have the excuse they didn't realise they weren't loading them. Secondly, you need to make really sure that your /etc/mongorc.js is valid and correct.


Outside the shell one of the smaller changes could save you a few keystrokes or cost you a few key if you don't know about it. The mongoimport command takes as an option --collection (also -c), which lets you specify which collection the imported data is going into – in 2.4 and before it was pretty much mandatory. In MongoDB 2.6, you can drop the --collection option and the importer will use the name of the file you are importing from as the name of the collection. So, you can save keystrokes by exporting your data to a file with the collection name its destined for (or be caught out when you accidentally forget the -c option and wonder where that new collection in your database came from).


If you've ever felt that the MongoDB executables were somewhat noisy, the --quiet option may bring some calm to your day. In 2.6 the --quiet option, which suppresses all logging messages except errors, is supported by all the MongoDB command-line tools rather than a select few.

Dj Walker-Morgan
Dj Walker-Morgan is Compose's resident Content Curator, and has been both a developer and writer since Apples came in II flavors and Commodores had Pets. Love this article? Head over to Dj Walker-Morgan’s author page to keep reading.

Conquer the Data Layer

Spend your time developing apps, not managing databases.