mongodb-promises
v0.2.0
Published
A simple promise based wrapper for mongodb
Downloads
34
Maintainers
Readme
mongodb-promises
A simple promise wrapper around mongodb(peer dependency) nodejs drivers.
How to use it?
In a big app.
- Create a file may be db.js and put below code into that.
var config = require('config'),
db = require('mongodb-promises').db(config.db.host, config.db.name);
module.exports = db;
- Now you can use db.js any no. of time time making sure all sharing same db object instance.
var db = require('./db')
todoColl = db.collection('todos');
todoColl.insert([{text: 'first task to do'}, {text: 'second task to do'}])
.then(function (resultArr) {
console.log('saved successfully');
})
.catch(function (err) {
console.error('Error on insert ', err);
});
Simple one file script
var db = require('mongodb-promises').db('host:port', 'db_name'), // host can be array in case of replSet
todoColl = db.collection('todos');
todoColl.insert([{text: 'first task to do '}, {text: 'second task to do'}])
.then(function (resultArr) {
console.log('saved successfully');
})
.catch(function (err) {
console.error('Error on insert ', err);
});
List of DB methods
- collection(collectionName, options) Returns a collection object to perform operations on a collection using promises.
- drop() To drop database, call it carefully.
- createCollection(name, options) Creates a collections on mongodb, usefull if you want collections be created before use(Mongodb can create collection if not exist on first document creation).
List of Collection methods
insert(doc, options) Insert single or array of document(s) mongodb api
remove(selector, options) Remove dcument(s) mongodb api
rename(newName) Rename collection mongodb api
save(doc, options). mongodb api
update(selector, doc, options) mongodb api
updateUpsert(selector, doc) Update Or upsert document.
updateMulti(selector, doc) Update multiple records.
distinct(key, query, options) mongodb api
count(filter) mongodb api
countAll() Count all documents in collection.
drop() Drop collection, use it carefully mongodb api
findAndModify(query, sort, doc, options) mongodb api
findAndModifyOrUpsert(query, sort, doc)
findAndRemove(query, sort, options) mongodb api
find(query, options) mongodb api
findOne(query, options) mongodb api
mapReduce(map, reduce, options) mongodb api
group(keys, condition, initial, reduce, finalize, command, options) mongodb api
options() mongodb api
isCapped() mongodb api
geoNear(x, y, options) mongodb api
geoHaystackSearch(x, y, options) mongodb api
aggregate(array, options) mongodb api
stats() mongodb api
indexes() mongodb api
createIndex(fieldOrSpec, options) mongodb api
ensureIndex(fieldOrSpec, options) mongodb api
indexInformation(options) mongodb api
dropIndex(name) mongodb api
dropAllIndexes() mongodb api
reIndex() mongodb api
indexExists(indexNames) mongodb api
Changes Log
0.2.0
- Mongodb package as peer dependency.