0

I want to add a super user to admin database from MongoDB using NodeJS. My first try is this:

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

var db = new Db('admin', new Server('locahost', 27017));

// Establish connection to db
db.open(function(err, db) {
    if (err) { return console.log(err); }

    console.log("Opened database");

    // Add a user to the database
    db.addUser('superuser', '1234', {
        roles: [
            "userAdminAnyDatabase",
            "dbAdminAnyDatabase",
            "clusterAdmin",
            "readWriteAnyDatabase"
        ]   
    }, function(err, result) {
        if (err) { return console.log(err); }
        console.log("Added.");
    });
});

When running the script I got this error:

[Error: failed to connect to [locahost:27017]]

And before this:

========================================================================================
=  Please ensure that you set the default write concern for the database by setting    =
=   one of the options                                                                 =
=                                                                                      =
=     w: (value of > -1 or the string 'majority'), where < 1 means                     =
=        no write acknowledgement                                                       =
=     journal: true/false, wait for flush to journal before acknowledgement             =
=     fsync: true/false, wait for flush to file system before acknowledgement           =
=                                                                                      =
=  For backward compatibility safe is still supported and                              =
=   allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}]      =
=   the default value is false which means the driver receives does not                =
=   return the information of the success/error of the insert/update/remove            =
=                                                                                      =
=   ex: new Db(new Server('localhost', 27017), {safe:false})                           =
=                                                                                      =
=   http://www.mongodb.org/display/DOCS/getLastError+Command                           =
=                                                                                      =
=  The default of no acknowledgement will change in the very near future                =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================

How can I fix the script to add the user superuser with password 1234 to admin database from MongoDB?

11
  • Administrative operations like adding an admin should rather be done through the mongo shell, not through a program. Commented Dec 13, 2013 at 9:17
  • @Philipp So, I guess the best alternative would be to run shell commands from Nodejs side... Right? Commented Dec 13, 2013 at 9:18
  • When your error is [Error: failed to connect to [locahost:27017]] it likely means that the problem occurs long before you try to create a new user. So when you wonder how to create a user, you are searching in the wrong direction. Commented Dec 13, 2013 at 9:19
  • 2
    new Server('locahost', 27017), Do you see any problem here? Look closely! Typo in localhost name Commented Dec 13, 2013 at 9:26
  • 1
    @Sriharsha Good point! Look in the docs. Same typo. ;-) Commented Dec 13, 2013 at 9:28

1 Answer 1

2

Your mistake is in this line:

 var db = new Db('admin', new Server('locahost', 27017));

You made a typo. What you mean is localhost.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.