mongodb-promises

0.2.0 • Public • Published

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

Changes Log

0.2.0

  • Mongodb package as peer dependency.

Package Sidebar

Install

npm i mongodb-promises

Weekly Downloads

2

Version

0.2.0

License

ISC

Last publish

Collaborators

  • ankitpatial