Skip to content

Instantly share code, notes, and snippets.

@agius
Forked from danielgwood/gist:1244903
Created December 12, 2011 19:26
Show Gist options
  • Save agius/1468681 to your computer and use it in GitHub Desktop.
Save agius/1468681 to your computer and use it in GitHub Desktop.
MongoDB shell cheatsheet
// A bunch of functions you can use in the MongoDB shell
// taken from http://api.mongodb.org/js/1.8.2/index.html
// amongst other sources
// Used in place of print() to view the contents of objects
printjson(x);
printjsononeline(x);
// File system interaction
ls(...);
pwd();
cd(...);
removeFile(...);
mkdir(...);
// Misc
hex_md5('foo');
//basic query
db.stream_objs.find().sort({created_at: -1}).limit(1)
// advanced queries at http://www.mongodb.org/display/DOCS/Advanced+Queries
// ops
db.currentOp()
db.getLastError()
db.mycollection.runCommand("compact")
//replica set commands
!!!rs.initiate() // NEVER run this except ONCE when you start your FIRST DB
// if one rs adds another machine with its own rs,
// it will wreck your life
rs.slaveOK()
rs.status()
var cfg = rs.config() // gets current replica set configuration
delete cfg.members[0].hidden // deletes a property from the configuration hash
rs.reconfig(cfg) // replaces current configuration with cfg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment