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()
})
})
})
@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