Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
REST Server for MongoDB (using node.js)
JavaScript
Pull request Compare This branch is 2 commits ahead of adomado:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
commands
controllers
dist
node_modules
tests
views
.gitignore
LICENSE
README.md
app.js
config.json
package.json
server.js

README.md

Credits

https://github.com/ActiveUnits/mongodb-rest

Name

mongodb-rest2 - REST server for MongoDB

Description

This is a REST server for MongoDB using Node, using the native node.js MongoDB driver.

Major update, now using Express

Installation

Installation is now via npm: npm install http://github.com/ActiveUnits/mongodb-rest/tarball/master.

To Run

node ./bin/mongodb-rest2

API

Supported REST requests:

  • GET /db/collection - Returns all documents
  • GET /db/collection?query=urlencoded({isDone: false}) - Returns all documents satisfying query
  • GET /db/collection?query=urlencoded({isDone: false})&limit=2&skip=2&sort=urlencoded(sortQuery) - add options to query (limit, skip & sort are supported atm)
  • GET /db/collection/id - Returns document with id
  • GET /db/collection/id?deep=true - Returns document with id and fetches all its deep nested object references
  • POST /db/collection - Insert new document in collection (place insert json document in POST body)
    • when you need to post Object Reference send it as {namespace: "collection", oid: "123dsas2sdasd"}
  • PUT /db/collection/id - Update document with id (place update json document in PUT body)
    • when you need to update Object Reference send it as {namespace: "collection", oid: "123dsas2sdasd"}
    • when you send {"field.innerField":"..."} it will automaticly be converted to {field:{innerField:"..."}
  • PUT /db/collection?query=urlencoded({isDone: false}) - update will get executed upon all documents in the collection
  • DELETE /db/collection/id - Delete document with id
  • DELETE /db/collection?query=urlencoded({isDone: false}) - Delete all documents matching given query

Additions:

  • method overriding:
    • for PUT set header X-HTTP-Method-Override = PUT
    • for DELETE set header X-HTTP-Method-Override = DELETE
    • for POST set header X-HTTP-Method-Override = POST
  • mongodb cluster connectivity (by providing array of { post: , host: } objects in config.json

Special :

  • GET /timestamp - Returns server timestamp in jsonp (/timestamp?callback=foo)

REST responses:

  • all responses are containing json({success: true or false, data: the data to be returned or which has been submitted})

Update Example

Update every male unicorn, increment hits by 1

$j.ajax({  
  url : "http://localhost:3000/test/unicorns?query=" + encodeURI(JSON.stringify({gender : "m"})),   
  data : JSON.stringify({'$inc' : {hits: 1}}),   
  type : "PUT",   
  contentType : "application/json",   
  success : function(d) {     
    console.log("Success");     
    console.log(d);   
  },   
  error : function(d) { 
    console.log("error");     
    console.log(d)   
  } 
});

Usage

config.json

{
    "dbconnection": {
        "port": 27017,
        "host": "localhost"
    },
    "port": 3000,
    "mode": "development",
    "autoStart": true
}

standalone

npm install http://github.com/ActiveUnits/mongodb-rest/tarball/master -g
mongodb-rest2
  • Note that this will launch the mongodb-rest server by looking into the current directory for config.json
  • You can alter the config location by passing --config /path/to/config.json

inside node.js application

var server = require("mongodb-rest2/server")
var options = {
    "db": {
        "port": 27017,
        "host": "localhost",
        "name": "test-db"
    },
    "port": 3000,
    "mode": "development",
    "autoStart": false
};
var expressApp = server.create(options).start();

hooking to mongodb-rest2 (secure mongodb-rest)

var server = require("mongodb-rest2/server")
var options = {
    "db": {
        "port": 27017,
        "host": "localhost",
        "name": "test-db"
    },
    "port": 3000,
    "mode": "development",
    "autoStart": false
};
var secureEveryRequestMiddleware = function(req, res, next) {
    if(req.param("token") != "myAuthToken") {
        res.send("Sorry", 401);
    } else
        next();
}; 
var expressApp = server.create(options, {
    "pre-router": function(expressApp) {
        expressApp.use(secureEveryRequestMiddleware);
    }
}).start();

using raw rest commands

var target = { db: "name", collection:"name, connection: {host: "localhost", port: 27017} };

var createCommand = require("mongodb-rest2/commands/create");
createCommand(target, { field1: value1 }, function(err, doc) { });

var updateCommand = require("mongodb-rest2/commands/update");
updateCommand(target, { _id: value }, { field1: value1 }, { multi: true, safe: true }, function(err, docs) { });

var listCommand = require("mongodb-rest2/commands/list");
listCommand(target, { field1: value1 }, { limit: 10, skip: 20 }, function(err, docs) { });

var deleteCommand = require("mongodb-rest2/commands/deleteCommand");
deleteCommand(target, { field: value }, { safe: true }, function(err, docs) { } ); 

Testing

Testing is now done using vows/api-easy. Just run vows ./tests/*.js in the main folder while you have the mongodb-rest2 running at port 3000

Credits

Something went wrong with that request. Please try again.