droopy-mongo
v1.0.3
Published
Wrapper for native mongodb driver to promisify things
Downloads
7
Readme
droopy-mongo
Wrapper for native mongodb driver to return promises.
npm install droopy-mongo
// Initialize
var mongo = require("droopy-mongo");
var dao = new mongo.MongoDao(config.mongo.url);
var topics = dao.collection("topics");
// Utilize
topics.find({ category: "Mongo"}).then(function(results) {
console.log(results);
});
Methods
Collection
collection.find(query)
-collection.find(query, fields)
- fields param limit the fields to returncollection.find(query, fields, options)
- options let you specify things like sorting and limitscollection.findOne(query)
- returns a single objectcollection.checkIfExists(query)
- returns true if the query has at least one resultcollection.insert(item)
- item can be either a single object or an array of objects (for a batch insert)collection.update(query, updates)
- peforms a merge, only updates properties specified in theupdates
param.collection.updateOne(query, updates)
- performs a merge on the first item that matches the querycollection.overwrite(query, item)
- overwrites all matching documents with the passed in itemcollection.remove(query)
- deletes all items that match the query
//EXAMPLES
var mongo = require("droopy-mongo");
var dao = new mongo.MongoDao(config.mongo.url);
var topics = dao.collection("topics");
topics.find({title: "test"}).then(function(results){
console.log("Found %s 'test' matches", results.length);
});
topics.insert({title:"test"}).then(function(doc) {
console.log("Inserted:");
console.log(doc);
});
topics.findOne({title: "test"}).then(function(result){
console.log("Looking for one. Found: ");
console.log(result);
});
topics.checkIfExists({title: "test"}).then(function(result){
console.log("Checking if 'test' exists. Answer is: %s", result);
});
topics.overwrite({title: "test"}, {title:"test2"}).then(function(updatedItem){
console.log("Updated 'test' to 'test2', full overwrite");
});