Skip to content
This repository

MongoDB shell written in Node.js

tree: c2296414a3

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 bin
Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 History.md
Octocat-spinner-32 LICENSE
Octocat-spinner-32 Makefile
Octocat-spinner-32 README.md
Octocat-spinner-32 index.js
Octocat-spinner-32 package.json
README.md

gomon

MongoDB shell written in Node.js

Experimental

Usage

$ gomon --help

  Usage: gomon [options] [db address] [file names]

  Options:

    -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
    192.169.0.5/foo         foo database on 192.168.0.5 machine
    192.169.0.5:9999/foo    foo database on 192.168.0.5 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.

Example

$ gomon mongodb://localhost:27017,hostA:27017,hostB/dev
gomon version 0.0.3
connecting to mongodb://localhost:27017,hostA:27017,hostB/dev
gomon>

API

  • 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.

Example:

First we connect to fakehost.

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

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
system.indexes
users
products

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)
gomon>
error:  null
{ _id: 50f99acfc50ea8c8ef23142c,
  name: 'gomon was here' }

Files

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

// script1.js
console.log('gomongo');

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

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;.

Eval

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

bye

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

Debugging

Enable debugging output:

DEBUG=gomon gomon localhost:27017

LICENSE

Something went wrong with that request. Please try again.