简体   繁体   English

Nodejs + Mongo数据库使用用户名和密码连接服务器数据库

[英]Nodejs + Mongo db connect with server database with username and password

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. 我正在使用mongodb npm模块。

db.js 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 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. 我期望使用相同的mongodb npm模块。 Don;t want to use mongoose because its already implemented many api;s in this project.. 不要使用猫鼬,因为它已经在该项目中实现了许多api。

Expect result : connect server db with username and password.. Or have any alternate methods? 预期结果:用用户名和密码连接服务器数据库。.还是有其他方法?

Just use Mongo's Connection URL, as described in mogo's docs: 只需使用mogo的文档中所述的Mongo连接URL:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

So your code would look like this (accordingly with mongodb's npm module manual ): 因此,您的代码将如下所示(根据mongodb的npm模块手册 ):

const MongoClient = require('mongodb');

// Connection URL
const url = 'mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]]';

// Database Name
const dbName = '[dbName]';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

Best luck! 好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM