mongodb-rest2 - REST server for MongoDB
This is a REST server for MongoDB using Node, using the native node.js MongoDB driver.
Major update, now using Express
Installation is now via npm: npm install http://github.com/ActiveUnits/mongodb-rest/tarball/master
.
Supported REST requests:
GET /db/collection
- Returns all documentsGET /db/collection?query=urlencoded({isDone: false})
- Returns all documents satisfying queryGET /db/collection?query=urlencoded({isDone: false})&limit=2&skip=2
- add options to query (limit, skip are supported atm)GET /db/collection/id
- Returns document with idGET /db/collection/id?deep=true
- Returns document with id and fetches all its deep nested object referencesPOST /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"}
- when you need to post Object Reference send it as
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:"..."}
- when you need to update Object Reference send it as
- PUT /db/collection?query=urlencoded({isDone: false}) - update will get executed upon all documents in the collection
DELETE /db/collection/id
- Delete document with idDELETE /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
REST responses:
- all responses are containing
json({success: true or false, data: the data to be returned or which has been submitted})
{
"dbconnection": {
"port": 27017,
"host": "localhost"
},
"port": 3000,
"mode": "development",
"autoStart": true
}
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
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();
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();
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 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