Creating Users from Java

34 views
Skip to first unread message

Rich C

unread,
Jul 10, 2014, 1:02:37 PM7/10/14
to mongod...@googlegroups.com
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.



Rich C

unread,
Jul 10, 2014, 3:01:13 PM7/10/14
to mongod...@googlegroups.com
A day late, but I did finally find the correct documentation:  http://docs.mongodb.org/manual/reference/command/createUser/#dbcmd.createUser


Reply all
Reply to author
Forward
0 new messages