Nodejs + Mongo数据库使用用户名和密码连接服务器数据库 [英] Nodejs + Mongo db connect with server database with username and password

查看:114
本文介绍了Nodejs + Mongo数据库使用用户名和密码连接服务器数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个现有的项目.这里使用多个数据库.我正在努力用用户名和密码连接服务器数据库.

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.

我正在使用mongodb npm模块.

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.

注意.我期望使用相同的mongodb npm模块.不要使用猫鼬,因为它已经在该项目中实现了许多api.

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?

推荐答案

只需使用Mongo的连接URL,如 mogo的文档:

Just use Mongo's Connection URL, as described in mogo's docs:

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

因此您的代码应如下所示(根据 mongodb的npm模块手册):

So your code would look like this (accordingly with mongodb's npm module manual):

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();
});

祝你好运!

这篇关于Nodejs + Mongo数据库使用用户名和密码连接服务器数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆