如何在MongoDB Java Driver 2.13中添加和删除用户?

时间:2022-02-14 12:51:36

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

使用终端,我可以使用db.createUser(user,writeConcern)添加用户并使用db.dropUser(username,writeConcern)删除它

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

如何在MongoDB Java Driver 2.13中添加和删除用户?

2 个解决方案

#1


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

在MongoDB中,Java Driver 2.13与shell非常相似。要添加用户,您可以使用DB.addUser并删除可以使用DB.removeUser的用户 - 请参阅api文档以获取参数

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

应该注意的是,这些方法已被弃用,将来很可能会在4.0版本中删除。

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.

首选方法是直接使用命令。因此,您可以使用DB.command - 命令的语法可以在常规MongoDB文档中找到,请参阅createUser并参阅dropUser。

#2


Removes the user from the current database.

从当前数据库中删除用户。

> db.dropUser(username, writeConcern)

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

username - 要从数据库中删除的用户的名称。

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

writecConcern - 可选。删除操作的写入级别。 writeConcern文档采用与getLastError命令相同的字段。

Example:

use products
db.dropUser("reportUser1", {w: "majority", wtimeout: 5000})

#1


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

在MongoDB中,Java Driver 2.13与shell非常相似。要添加用户,您可以使用DB.addUser并删除可以使用DB.removeUser的用户 - 请参阅api文档以获取参数

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

应该注意的是,这些方法已被弃用,将来很可能会在4.0版本中删除。

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.

首选方法是直接使用命令。因此,您可以使用DB.command - 命令的语法可以在常规MongoDB文档中找到,请参阅createUser并参阅dropUser。

#2


Removes the user from the current database.

从当前数据库中删除用户。

> db.dropUser(username, writeConcern)

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

username - 要从数据库中删除的用户的名称。

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

writecConcern - 可选。删除操作的写入级别。 writeConcern文档采用与getLastError命令相同的字段。

Example:

use products
db.dropUser("reportUser1", {w: "majority", wtimeout: 5000})