Organizational Research By

Surprising Reserch Topic

how to connect to another mongodb database as superuser using nodejs


how to connect to another mongodb database as superuser using nodejs  using -'node.js,security,mongodb'

The solution to this problem works fine:

Instead of doing:

$ mongo my_db_name -u superuser -p 1234


I do

$ mongo admin -u superuser -p 1234 # connecting as super user to admin db
> use anotherDb


in shell.



Which is the solution in NodeJS?

I tried to connect to mongodb://superuser:1234@localhost:27017/my_db_name but I get this error:


  { [MongoError: auth fails] name: 'MongoError', code: 18, ok: 0, errmsg: 'auth fails' }


My code is:

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient;

MongoClient.connect("mongodb://superuser:1234@localhost:27017/my_db_name",
    function(err, db) {
       if (err) { return console.log(err); }
       console.log("Successfully connected.");
    }
);


Note that superuser is the omnipotent user that can write and read rights in any database.

If I do MongoClient.connect("mongodb://superuser:1234@localhost:27017/admin (replaced my_db_name with admin) it connects successfully. Why?

How can I connect to the my_db_name using superuser and the password (1234)?
    

asked Sep 30, 2015 by balvant maurya
0 votes
11 views



Related Hot Questions

2 Answers

0 votes
NULL
answered Sep 30, 2015 by akasati02
0 votes
NULL
answered Sep 30, 2015 by akhilesh

...