Hello. I am trying to create a new user to access a new DB. I connect using a root user to the admin db and then I try to create a new user on a new db…
MongoClient.connect( process.env.JEST_MONGODB_URL, function( err, client ) {
if ( err ) return console.error( err );
const admin = new Admin( client.db( process.env.MONGODB_DATABASE ) );
admin.addUser(
process.env.MONGODB_USERNAME,
process.env.MONGODB_PASSWORD, {
roles: [
{
role: 'readWrite',
db: process.env.MONGODB_DATABASE
}
]
},
function( err, result ) {
if ( err ) return console.error( err );
console.log( result );
client.close();
});
});
Im using here client.db()
and passing that to new Admin()
because I thought that would create the user in that db, but its creating it in the admin
db instead.
In the code above, JEST_MONGODB_URL
is a url like : mongodb://root:password123@mongodb-primary,mongodb-secondary:27017/admin?replicaSet=myset
amd MONGODB_DATABASE
is a brand new db name like fubar
When I use the mongo shell I can use fubar
and then create the new user and that works fine. How can I do this with mongodb native driver for node?