This may save other new Java/MongoDB developers some time. I wanted to create a user id for a DB from our application's Java code. It took a day of trial and error to figure out that the following JSON string is the one to pass into DB.command(DBObject):
{ "
createUser" : "myuser" , "pwd" : "mypwd" , "roles" : [ { "role" : "dbOwner" , "db" : "MongoTestDB"}]}
As far as I can tell, documentation only exists how to create user ids from the mongo shell. Here's what the
http://docs.mongodb.org/manual/tutorial/add-user-to-database/ page says:
db.createUser(
{
user: "reportsUser",
pwd: "12345678",
roles: [
{ role: "read", db: "reporting" },
{ role: "read", db: "products" },
{ role: "read", db: "sales" }
]
}
)
Since the Java driver doesn't have a createUser() method, I could only find the command() method to do the task.
The trick was substituting "createUser" for "user" in the JSON.
If someone knows a better way to do things, I'd love to hear it--Thanks.