Ionică Bizău
Ionică Bizău

Reputation: 113465

How to add user in Mongo admin database from NodeJS?

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?

Upvotes: 0

Views: 3229

Answers (1)

Philipp
Philipp

Reputation: 69703

Your mistake is in this line:

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

You made a typo. What you mean is localhost.

Upvotes: 2

Related Questions