I am working on an existing project. Here using multiple database. I am struggling to connect the server database with username and password.
Currently now I am using local database without username and password. It's working fine.
I am using mongodb npm module.
db.js
var mongodb = require('mongodb');
module.exports.init = function (callback) {
var server = new mongodbs.Server('localhost', 27017, {})
//var server = new mongodbs.Server('abc.com', 27017, username , password, {})
module.exports.db = {};
new mongodb.Db('user', server, {
w: 1
}).open(function (error, client) {
module.exports.user = client;
module.exports.user_tokens = client.collection('tokens');
module.exports.user_session = client.collection('session');
module.exports.user_consent = client.collection('consent');
module.exports.user_client = client.collection('client');
callback(error);
});
new mongodb.Db('employee', server, {
w: 1
}).open(function (error, client) {
module.exports.employee = client;
module.exports.employee_list = client.collection('list');
module.exports.employee_detail = client.collection('detail');
callback(error);
});
};
index.js
var mongoUtil = require('./db');
mongoUtil.init(function (error) {
if (error)
throw error;
});
router.post('/test', function (req, res) {
var collection = mongoUtil.employee_list;
collection.insert({
name: 'test'
}, function (err, item) {
if (!err && item) {
console.log("success")
} else {
console.log("failure")
}
});
});
Can any one please help to connect the server db with username and password.
Notes. I am expecting to use same mongodb npm module. Don;t want to use mongoose because its already implemented many api;s in this project..
Expect result : connect server db with username and password.. Or have any alternate methods?