Skip to content

Instantly share code, notes, and snippets.

@foull
Last active May 6, 2021 07:59
Show Gist options
  • Save foull/0834e34ea3d9174e2c4bfe36a44a14d0 to your computer and use it in GitHub Desktop.
Save foull/0834e34ea3d9174e2c4bfe36a44a14d0 to your computer and use it in GitHub Desktop.
NodeJS - MongoDB - creating new user with admin privileges
var MongoClient = require('mongodb').MongoClient
//
// config
//
var mongoPort = '27017'
var mongoHost = 'localhost'
var dbName = 'dbName'
var userName = 'admin'
var userPassword = 'pass1'
//
// start
//
MongoClient.connect('mongodb://'+mongoHost+':'+mongoPort+'/'+dbName,
function(err, db) {
if (err){
return console.log('Error: could not connect to mongodb')
}
// Use the admin database for the operation
var adminDb = db.admin()
// Add the new user to the admin database
adminDb.addUser(userName, userPassword, {
roles: [{
role : "userAdmin",
db : dbName
}]
},
function(err, result) {
if (err){
return console.log('Error: could not add new user')
}
// Authenticate using the newly added user
adminDb.authenticate(userName, userPassword, function(err, result) {
if (err){
return console.log('Error: could authenticate with created user')
}
console.log('Ok')
db.close()
})
})
})
@lkmali
Copy link

lkmali commented Feb 21, 2019

I am getting below error
node:24066) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
/home/laxman/CARTOS/main/node_modules/mongodb/lib/operations/mongo_client_ops.js:439
throw err;
^

TypeError: db.admin is not a function
at /home/laxman/CARTOS/main/deploy/singleClickTenant/mongo-create-user.js:26:26
at result (/home/laxman/CARTOS/main/node_modules/mongodb/lib/utils.js:414:17)
at executeCallback (/home/laxman/CARTOS/main/node_modules/mongodb/lib/utils.js:406:9)
at err (/home/laxman/CARTOS/main/node_modules/mongodb/lib/operations/mongo_client_ops.js:285:5)
at connectCallback (/home/laxman/CARTOS/main/node_modules/mongodb/lib/operations/mongo_client_ops.js:240:5)
at process.nextTick (/home/laxman/CARTOS/main/node_modules/mongodb/lib/operations/mongo_client_ops.js:436:7)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)

@rokossovski
Copy link

Since the mongodb driver version 3 you cannot access the admin db directly anymore. You either have to connect using the API v3 or install the old driver.

@horvathdeli
Copy link

I'm getting this error below at line:
adminDb.authenticate(userName, userPassword, function(err, result) {

TypeError: adminDb.authenticate is not a function
at eval (eval at (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\mongo-create-user.js:42:5), :1:13)
at C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\mongo-create-user.js:42:5
at result (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:75:17)
at session.endSession (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:64:11)
at ClientSession.endSession (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\core\sessions.js:135:41)
at executeCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:59:17)
at handleCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\utils.js:129:55)
at execute (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\add_user.js:86:16)
at handleCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\utils.js:129:55)
at db.s.topology.command (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\command.js:115:7)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment