mongosh admin --eval 'db.createUser({user: "app", pwd: "secret", roles: [{role: "readWrite", db: "appdb"}]})'

Category: Databases & SQL

mongosh admin --eval 'db.createUser({user: "app", pwd: "secret", roles: [{role: "readWrite", db: "appdb"}]})'

Create a database user

Creates user app with the readWrite role scoped to appdb, so it can read and write that database only. Roles are the MongoDB privilege model; add more roles for broader access. The user record lives in admin.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.