Skip to content

Instantly share code, notes, and snippets.

@skarra
Created October 4, 2013 06:38
Show Gist options
  • Save skarra/6821844 to your computer and use it in GitHub Desktop.
Save skarra/6821844 to your computer and use it in GitHub Desktop.
How to script the Mongo shell I have seen many of us type out long and convoluted mongo shell commands to view small world records. I learnt today that the mongo shell can be scripted using javascript and can be used to create short cuts for often used long command. For e.g. the attached file contains four helper routines to quickly look at a mo…
//
// A bunch of functions to explore the small world database. This is
// meant to be loaded into the mongo shell so that objects can be
// explored with minimal typing .
//
// Usage: There are two ways to use this:
//
// 1. Eval the script at mongo launch time itself:
//
// $ mongo sw.js --shell
//
// 2. Load the file after you have started the shell (this assumes you
// are in the same location as the script file.
//
// > load('sw.js')
//
function getId (x) {
db = connect("localhost:27017/smallworld");
return db.sw_collection.find({'_id' : ObjectId(x)});
}
function getId_p (x) {
db = connect("localhost:27017/smallworld");
return db.sw_collection.find({'_id' : ObjectId(x)}).pretty();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment