Personal project to query MongoDB using standard SQL in Mongo shell, with custom tab completion (WIP).
Queries can be constructed like so: db.sql('select * from collection').
When the results are returned, the original SQL, along with the converted MongoDB query, are displayed for convenience.
By default, query results are prettified, but you can have the uglified results returned by entering 1 after the SQL query (e.g. db.sql('select * from collection', 1).
Notes / Tidbits:
- When adding filters to your SQL query, you do not need to wrap string values in quotes.
- In order to protect against long-running queries, a default limit of 20 records is set. To override this, simply add
top Nto your query (see below).
Currently queries only support:
- Basic filtering (
=,<,>,<=,>=),and,or,in,like, ... - Not equal to (use
!=operator ...db.sql('select * from collection where field != value')) - Not in (use
!inoperator and simply separate values by a comma, with no parentheses). E.g.db.sql('select * from collection where field !in value,value')) - Projection (simply list fields after the
selectkeyword. To explicitly exclude fields, precede fieldnames with!like so:db.sql(select !field1, !field2 from collection'). - Top N
db.sql('select top 20 * from collection') - Order by (delimit conditions by comma)
db.sql('select * from collection order by field1 asc, field2 desc')
As an example, the sample Northwind database (credit to tmcnab https://github.com/tmcnab/northwind-mongo) was used to illustrate the autocompletion.
- To see the autocompletion in action, start the Mongo server (
mongod), and runmongoin another terminal window - Clone or download the
northwind-mongoproject from the link above, cd into the directory, and run themongo-import.shfile from the terminal
- Once the database has been imported, type
nwand press tab. You should see it expand touse Northwind - Now, type
seland press tab. You should see that expand todb.select * from - Next, type
oand tab. You should see a list of all the collections that begin witho(in this case order-details and orders).
- You can also skip
seland type the first letter(s) of the collection (in this caseo) to bring up the same prompt. After a collection is selected, the terminal will populate withdb.sql(select * from [selected collection].
- To auto-complete the collection name, enter the number listed and press tab again.
- An ascii table (credit to https://github.com/sorensen/ascii-table) of the collection fields will be printed out to remind you what fields are available.
- This table can also be generated by calling .fields() on a given collection (
db.orders.fields())
- Type
wand you will see it expand towhere. - Type space followed by the first letter of one of the field names, like
d(casing doesn't matter). - All of the fields that begin with
dwill be listed, which can be selected by entering the number listed next to the field and pressing tab. - Finally, complete the query and remember to add the closing quote and parenthesis (which can also be auto-completed by tabbing after space).
- Copy .mongorc.js into your home directory
cp .mongorc.js ~
To generate collection information for your own database (replicasets not currently supported):
- Open Terminal and cd into this project directory.
- Run
sudo mkdir /usr/local/scriptsto create a scripts directory inside /usr/local.- This will allow .mongorc.js to access script files.
- If you plan to use Underscore in mongo shell, you can run
cp mesh.js /usr/local/scripts. - Install node.js (https://nodejs.org), if you haven't already and run
npm installto install dependencies. - Run
node getSchema.js HOSTNAME PORT DATABASE_NAME- Example:
node getSchema.js localhost 27017 Northwind - This script will run your database through the variety.js schema analyzer (https://github.com/variety/variety) and save the output to usr/local/scripts/dbInfo.js.
- Example:
See snippetMap global variable on line 14.
Credit to tylerbrock / mongo hacker http://tylerbrock.github.com/mongo-hacker for result coloring (which is awesome).



