aix-mongo

1.0.3 • Public • Published

aix-mongo

Node.js module for managing mongodb

This is a module that you can easily manage your mongodb database for inserting, updating, finding or removing your documnets.

If there is any problem or if you think it's better to update or add another functionality to the module please let me know, and help me to improve this simple and small package.

Installation

$ npm install aix-mongo

Usage

var db = require('aix-mongo');

Adding ang important option

db.init({dbAddress: "mongodb://localhost:27017/Your-database-name"});

Where your-database-name is your db.

Usage

Usage of aix-mongo is very simple. There is just a few functions.

Converting string id to an object id

db.objId(string);
// Usage example in a remove function.
db.remove("clients", {_id:db.objId(req.body.id)}, function(){
    console.log("Client removed!");
});
// Or handy write and id
db.remove("clients", {_id:db.objId("56798c9b4a2572512542145f")}, function(){
    console.log("Client removed!");
});
 

Finding a document/s

db.find("collection-name", [filters], function(result){
    // your code goes here.
});
Example:

All parameters in [] are optional. Resutls are as arrays of objects.

db.find("collection-name", {name:"john", lname:"kangari"}, function(result){
    console.log(result[0].email);
    // Result: john.kangari@gmail.com
});

Insert document into collection

db.ins("collection-name", {data}, function(result){
    // your code goes here.
});

Update document/s

db.update("collection-name", {credential}, {new-data}, function(result){
    // your code goes here.
});
// Example with result:
db.update("clients", {name:"john"}, {email:"example@domain.com"}, function(result){
    console.log(result.nModified);
// Result: 1
});
 
// Example with result function:
db.update("clients", {name:"john"}, {email:"example@domain.com"}, function(){
    console.log("Update done successfully.");
});
 
// Example without any function:
db.update("clients", {name:"john"}, {email:"example@domain.com"});

Remove a document/s

db.remove("collection-name", {filters}, function(){
    // Your code goes here...
});
//Simple example of usage
db.remove("clients", {name:"john"}, function(){
    console.log("The client removed.");
});

This is a quick start documentation, if any more detail needed please refer to the GitHub page and let me know.

Package Sidebar

Install

npm i aix-mongo

Weekly Downloads

1

Version

1.0.3

License

GPL-3.0

Last publish

Collaborators

  • john.kangari