aix-mongo
Version:
A module for managing mongo DB.
78 lines (69 loc) • 2.2 kB
JavaScript
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
function init(options) {
connectionM = options.dbAddress || {};
}
function objId(id) {
return new ObjectId(id);
}
function find(collection, criteria, result) {
if (result == undefined) {
result = criteria;
criteria = {};
}
// Connect to the db
MongoClient.connect(connectionM, function(err, db) {
if (!err) {
db.collection(collection).find(criteria).toArray(function(err, docs) {
db.close();
result(docs);
}); // retrieve process
} // catch error
}); // database connection
}
var ins = function(collection, insData, result) {
// Get the documents collection
MongoClient.connect(connectionM, function(err, db) {
if (!err) {
// Insert some documents
db.collection(collection).insert(insData, function(err, insResult) {
db.close();
result(insResult);
}); // insert process
} // catch errro
}); // database connection
}
var update = function(collection, findData, newData, result) {
// Get the documents collection
MongoClient.connect(connectionM, function(err, db) {
if (!err) {
// Insert some documents
db.collection(collection).update(findData, {
$set: newData
}, function(err, updateResult) {
db.close();
result(updateResult);
}); // update process
} // catch errro
}); // database connection
}
var remove = function(collection, rmData, result) {
// Get the documents collection
MongoClient.connect(connectionM, function(err, db) {
if (!err) {
// Insert some documents
db.collection(collection).remove(rmData, function(err, rmResult) {
db.close();
result(rmResult);
}); // insert process
} // catch errro
}); // database connection
}
module.exports = {
init: init,
find: find,
ins: ins,
remove: remove,
update: update,
objId: objId,
}