Mongo-CRUD-Layer
A simple CRUD-interface for the node-mongodb-native driver. Abstracts the creation, reading, updating and deleting of documents into more comfortable methods.
Latest added feature:
- support for GridFS / GridStore
Installation
npm install mongo-crud-layer
or add it to your package.json
.
Usage
1. Instantiation
First, create a new MongoCrud instance to store the databases URI and (optional) some options.
var mongocrud = uri options false;
- uri - the URI to MongoDB, e.g. mongodb://localhost:27017/mongocrud-test
- options - optional settings
- gridFS - set to true, if objects exceed MongoDB document limit of 16mb, to store objects with GridFS mechanism
var MongoCrud = ;var mongocrud = ; // or var mongocrud = 'mongodb://localhost:27017/mongocrudtest';// orvar mongocrud = 'mongodb://localhost:27017/mongocrudtest' {} true;...
2. API
The API offers the following methods:
create(obj, collection, callback)
Creates a MongoDB document from the given object and stores it into the given collection. Returns the _id of the stored document.
- obj - the object to store
- collection - the collection to store the object in
- callback - a callback function with two parameters: error and the ObjectID of the stored object
var DB_URI = 'mongodb://localhost:27017/mongocrud-test';var COLLECTION = 'objectStore';var OBJ = name: 'Athyrion';var ID;...mongocrud;...
read(criteria, collection, callback)
Searches the given collection for documents matching the given criteria and returns the found documents. The criterium should be in most cases using the _id from the creation process, e.g. criteria = {_id: _id}.
- criteria - the criteria to search for in the database, usually the object's _id
- collection - the collection to search in
- callback - a callback function with two parameters: error and the found documents
...mongocrud;...
readAll(collection, callback)
Returns all documents stored in the given collection as an array. !!! Not available in GridFS mode !!!
- collection - the collection to search in
- callback - a callback function with two parameters: error and an array of objects from the collection
...mongocrud;...
update(criteria, obj, collection, callback)
Replaces the object in the given collection with given object. !!! Not available in GridFS mode !!!
- criteria - the criteria to search for in the database, usually the object's _id
- obj - the replacing object
- collection - the collection to search in
- callback - a callback function with two parameters: error and a mongodb result object
... OBJname = 'Athyrion-Westeros'; mongocrud;...
delete(critera, collection, callback)
Deletes the document that matches the given criteria.
- criteria - the criteria to search for in the database, usually the object's _id
- collection - the collection to search in
- callback - a callback function with two parameters: error and a mongodb [result object](http://docs.mongodb.org/manual/reference/command/insert/#insert-command-output
...mongocrud;...
close(callback)
Closes the connection to the database.
- callback - the callback function receiving null, if the closing was successful, otherwise a MongoError
...mongocrud;