Java+Database - Add Scroll to existing user via Java

Asked

Viewed 38 times

4

Mongodb use with Java and accurate, through Java run the following command on mongoDB:

db.grantRolesToUser( "joao", [ {role:"dbOwner",db:"loja"} ] )

That is, I need to add a scroll to an existing user, and I need to specify what the database is. After a lot of cracking up, I still can’t find the right way. Remembering that in the command I need to specify which is the database (as above), because at that time I will be logged in to the admin database, so I need to say that the command refers to the database store. I keep looking. But if anyone knows and can give me a clue, I’d appreciate!

1 answer

3


I found the answer:

MongoClient client = new MongoClient(new MongoClientURI("mongodb://127.0.0.1:27017"));

MongoDatabase adminDatabase = client.getDatabase("admin");

adminDatabase.runCommand(new Document("grantRolesToUser", "paulo")
.append("roles", Collections.singletonList(new Document("role", "dbOwner").append("db", "loja"))));

adminDatabase.runCommand(new Document("grantRolesToUser", "paulo")
.append("roles", Collections.singletonList(new Document("role", "dbOwner").append("db", "comercio"))));

client.close();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.