Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add user in Mongo admin database from NodeJS?
    primarykey
    data
    text
    <p>I want to add a super user to <code>admin</code> database from MongoDB using NodeJS. My first try is this:</p> <pre><code>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."); }); }); </code></pre> <p>When running the script I got this error:</p> <blockquote> <p><code>[Error: failed to connect to [locahost:27017]]</code></p> </blockquote> <p>And before this:</p> <pre><code>======================================================================================== = Please ensure that you set the default write concern for the database by setting = = one of the options = = = = w: (value of &gt; -1 or the string 'majority'), where &lt; 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 = ======================================================================================== </code></pre> <p>How can I fix the script to add the user <code>superuser</code> with password <code>1234</code> to <code>admin</code> database from MongoDB?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload