MongoDB shell written in Node.js


MongoDB shell written in Node.js


$ gomon --help
  Usage: gomon [options] [db address] [file names]
    -h, --help        output usage information
    -V, --version     output the version number
    --port <port>     port to connect to
    --host <host>     server to connect to
    -e --eval <code>  evaluate script
    -v --version      print gomon's version
    --shell           run the shell after executing files
  db address:
    foo                     foo database on local machine         foo database on machine    foo database on machine on port 9999
    mongodb://host:port/db  mongo URI
  file names:
    List of space delimited files (ending in .js) to execute.
    When passed, the shell exits after execution unless --shell
    is specified.
$ gomon mongodb://localhost:27017,hostA:27017,hostB/dev
gomon version 0.0.3
connecting to mongodb://localhost:27017,hostA:27017,hostB/dev
  • show.dbs: List available databases
  • show.collections: List available collections in current database
  • show.tables: Alias of show.collections
  • use[databaseName]: Switch databases
  • exit: Exits the shell
  • db.close(): Closes the database connection
  • db.col(): Accesses a collection
  • db.cols(): Retreives an array of collection names in the db
  • db.createCol(): Creates a collection
  • db.drop(): Drops the database
  • db.help(): Logs help text for all db methods
  • db.inspect(): Returns the name of the database
  • db.open(): Opens the database connection
  • db.runCommand(): Runs a command on the database
  • db.use(): Changes to a different database
  • db.collection.findOne(): Find a single document

More to come.

First we connect to fakehost.

$ gomon --host fakehost
gomon version 0.0.3
connecting to mongodb://fakehost

Next we list the available databases()

gomon> show.dbs
test                    0.203125GB
fake                    0.203125GB
dev                     0.203125GB

Switch to the dev database

gomon> use.dev

For autocompletion, type use.<TAB> to see a list of available databases.

Display the dev database collections

gomon> show.collections

Get a count of the users collection

gomon> db.users.count(console.log)
gomon> null 1

Look up a user

gomon> db.users.findOne(console.log)
gomon> null { _id: 50f99acfc50ea8c8ef23142c, name: 'gomon was here' }

p is a global helper for queries that performs pretty printing on results.

gomon> db.users.findOne(p)
error:  null
{ _id: 50f99acfc50ea8c8ef23142c,
  name: 'gomon was here' }

All arguments ending in .js will be treated as files and executed.

// script1.js 
// command line 
$ gomon script1.js
gomon version 0.0.3
connecting to mongodb://localhost:27017/test 
  waiting for script to call `exit;` ...
  OR press Ctrl+D to quit immediately

When files are passed, the shell will not automatically exit. This is so that async operations within scripts are not stopped prematurely. Your scripts must manage themselves, e.g. when all operations are completed, call exit;.

Code may be evaled and printed to the shell by passing the -e or --eval flag.

$ gomon --eval "db.databaseName + ' is the default db'"
gomon version 0.0.3
connecting to mongodb://localhost:27017/test
test is the default db

The shell will immediately exit after script evaluation unless the --shell flag is passed.

Enable debugging output:

DEBUG=gomon gomon localhost:27017