mongo-easy

0.0.3 • Public • Published

mongo-easy.js

Making the use of MongoDB very simple.

Implementation Requirements

and your ready to go...

Implementation
Terminal

$ npm install mongo-easy --save

Javascript file

var mgo = require('mongo-easy');
 
 

Get an array of users
Create an object to hold the collection name, projection and query

var param = {
    collection: "registered",
    query: { 
        "firstname": 1,
        "lastname": 1, 
        "email": 1, 
        "_id": 0 
    },
    projection: {}
}
 

And simply call the following function and pass your param object into the getRecordArray function

mgo.getConnection('users',  function(db) {
    mgo.getRecordArray(db, param, function(doc) {
        console.log(doc);
    });
})

The above construct does two things:

  • connects to the database.
  • uses the connection to retrieve a required set of data

Other examples are : Get an array of records/documents returned one at a time.

mgo.getConnection('users',  function(db) {
    mgo.getEachRecord(db, param, function(doc) {
      console.log(doc);
  });
})

Get a single record/document

mgo.getConnection('users', function(db) {
  mgo.getOneRecord(db, param, function(doc) {
  console.log(doc);
  });
})

Insert a record/document

//create an object to insert into function
var userObj = {
    collection: "registered",
    record: { 
        "firstname": "Bob",
        "lastname": "Bar",
        "email": "bob@someplace.com"
    }
}
 
mgo.getConnection('users',  function(db) {
  mgo.insertOneRecord(db, userObj, function(result) {
    console.log(result);
  })
});

Edit configuration: Simply change the port or host.

//  /config/dbConfig.js
var config = module.exports = {
    mdb: "mongodb://",
    mhost: "127.0.0.1",
    mport: ":27017/"
};

Readme

Keywords

none

Package Sidebar

Install

npm i mongo-easy

Weekly Downloads

0

Version

0.0.3

License

none

Last publish

Collaborators

  • markogrady1