I’m in the process of setting up mongodb, and we use puppet to control the configuration of our servers.
I’ve got most things setup how I need them, but I need to create the user inside the mongo database.
I know how to do this using the mongo shell, and I know that I can do it using javascript / a .js using the command
db.addUser("username", "password"[, readOnly])
However, I have been unable to find a solid example of what is needed to do this in javascript. More to the point, I need to be able to add a user from the command line, using some sort of shell script.
If someone could
a) point me to some solid examples of using javascript with mongoDB and
b) how can I do this from the command line?
Mongo’s cli tells you itself how to use it with a js file
usage: mongo [options] [db address] [file names (ending in .js)]
For example:
Udpate:
db.addUser deprecated since 2.6
https://docs.mongodb.com/v2.6/reference/method/db.addUser/
use
db.createUserinstead:$ mongo mydb file.js