README
Description
An easy-to-use mongodb library that returns promise objects
Installation
npm install easy-mongo-promise --save
Quick Start
require this packages and create a instance
const Client = require('easy-mongo-promise');
const db = new Client(url: 'mongodb://localhost:27017/users');
// or it could be like this
const db = new Client({
url: 'mongodb://localhost',
// If not, the default port will be configured
prot: 27017,
dbName: 'users'
});
find option
const Client = require('easy-mongo-promise');
const db = new Client(url: 'mongodb://localhost:27017/users');
// a collection must be specified before all operations
// with db.from('collection name')
// find option receive four parameters
/**
* [find option,return Promise]
* @param {Object} findCondition [default: {}]
* @param {Object} sortCondition [default: {}]
* @param {Number} pageSize [default: 0]
* @param {Number} page [default: 0]
* @return {Promise} [description]
*/
db.from('info').find().then(res => {
console.log(res);
});
Other operations are similar
// insert
db.from('info').insert(insertData).then(res => {
console.log(res);
});
// update
// All you need to do is pass in the data you want to change
// Unwanted as {$set:{xx:xx}}
db.from('info').update(condition,data).then(res => {
console.log(res);
});
//delete
db.from('info').delete(condition).then(res => {
console.log(res);
});
Other unimplemented methods, can look like this
db.from('collection').__init()
.then(MongoClientObject => {
// MongoClientObject.dbc represents the current collection
MongoClientObject.dbc.find().sort({id:1}).toArray((err,res)=>{
// close collection
MongoClientObject.client.close();
console.log(res);
})
})
Keywords
mongodb promise