Home

Jeff Mesens edited this page Sep 19, 2016 · 21 revisions

sleepy.mongoose is a REST interface for MongoDB.

PREREQUISITES

  • MongoDB, which can be downloaded from http://www.mongodb.org
  • Pymongo, the MongoDB python driver (version 1.9 or greater). You can install this with easy_install:
$ sudo easy_install pymongo==2.7

There is also experimental SSL support in the current master (not version 0.01). To use it, you must install the Python OpenSSL package:

$ sudo easy_install pyOpenSSL

Note: sleepy.mongoose only works with Python 2.5 and higher.

GETTING STARTED

Download the latest version.

Start the server by running:

$ python httpd.py

Command line flags:

  • -d or --docroot allows you to specify the location of your files. Defaults to the sleepy.mongoose directory

USING SLEEPY.MONGOOSE

There is a quick tutorial you can use to get started on my blog.

Sleepy.Mongoose only uses GETs and POSTs right now.

URIs are of the form /db_name/collection_name/_command

Commands are always prefixes by underscores.

Query Example

To find all documents in the collection “users” in the database “website”, you would use the following:

http://localhost:27080/website/users/_find

You should make sure any options are URL escaped. You can easily do this with any JavaScript shell, including the mongo shell.

Escaping Characters

To query for {"x" : 1}, we have the string '{"x" : 1}'. We run encodeURI('{"x" : 1}') and get "%7B%22x%22%20%3A%201%7D". We can now paste this beautful string into our URL:

http://localhost:27080/website/users/_find?criteria=%7B%22x%22%20%3A%201%7D

Warning: Picky JSON Formatting Ahead

{'x' : 1} is not valid JSON. The Python JSON parser won’t allow single quotes (or no quotes) around key names: you must always use double quotes around keys. For example, this is valid: {"x" : 1}.

Also, types that you are used to using in other drivers (e.g., Dates) are not part of JSON, so they have to be represented in a special way. See http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON.

Example:

In the shell, you might write:

{date : new Date()}

Date() is not part of JSON, so in Sleepy.Mongoose, you must write:

{date : {$date : _ms_since_epoch}}

NEXT STEPS

JQUERY

You can find useful and very simple jquery plugin here, it will also be useful if you want to make your own jquery code to use with jstree or other jquery plugins

TODO

  • Honey bunches of helpers: _ensure_index, listing databases, listing collections, dropping things
  • Handlers to get $oid, $date, etc. into a proper BSON types

TESTS

To run the tests, you must install restclient:

$ easy_install restclient

Then run:

$ python t/get.py
$ python t/post.py

TROUBLESHOOTING

If anything goes wrong, please email the MongoDB user list or file an issue.