0

Using terminal, I can add user using db.createUser(user, writeConcern)and remove it using db.dropUser(username, writeConcern)

How to add and remove users in MongoDB Java Driver 2.13?

Dev
  • 13,492
  • 19
  • 81
  • 174
  • 1
    Have you looked at the documentation [**Authenticate to MongoDB with the Java Driver**](https://docs.mongodb.org/ecosystem/tutorial/authenticate-with-java-driver/#authenticate-to-mongodb-with-the-java-driver) yet? – chridam May 05 '15 at 11:57
  • 1
    It only explains how to authenticate, a users may be created in MongoDB or come from other systems eg via GSSAPI – Ross May 08 '15 at 16:07

2 Answers2

0

In MongoDB Java Driver 2.13 its very similar to to the shell. To add a user you can use DB.addUser and to remove a user you can use DB.removeUser - see the api documentation for the arguments

It should be noted that these methods are deprecated and will be removed in the future most likely in the 4.0 release.

The preferred method is to use the commands directly. So you can use DB.command - the syntax for the commands can be found in the general MongoDB documentation, see createUser and see dropUser.

Ross
  • 17,861
  • 2
  • 55
  • 73
0

Removes the user from the current database.

> db.dropUser(username, writeConcern)

username - The name of the user to remove from the database.

writecConcern - Optional. The level of write concern for the removal operation. The writeConcern document takes the same fields as the getLastError command.

Example:

use products
db.dropUser("reportUser1", {w: "majority", wtimeout: 5000})
Abhay
  • 3,151
  • 1
  • 19
  • 29