I have a pipeline that executes commands and I want to use it to add a user to MongoDB. I have seen similar answers on stackoverflow, but those are for an older MongoDB version (they use .addUser()
instead of .createUser()
).
So far I have tried this:
'mongo {AUTH_URL} admin --eval "db.createUser({ createUser: {DB_USERNAME}, pwd: {DB_USER_PASSWORD}, roles: [{ role: "readWrite", db: {DB_NAME}}]})'
So what i need is a single line of code that will create the user for me and i will set all of the necessary elements through environment variables. If someone could provide an example of a terminal command to create a new user that would be great.
mongo
use admin
db.createUser({user:'admin',pwd:'admin',roles:
[{role:'root',db:'admin'}]})
use newdatabase
db.createUser({user:'database_user',pwd:'newdatabasepass',roles:[{role:'readWrite',db:'newdatabase'}]})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With