我正在尝试使用Scala驱动程序将用户添加到Mongo数据库。我使用以下代码来执行此操作:
val mongoClient = MongoClient()
val dbName = "..."
// create database
val db = mongoClient.getDatabase(dbName)
val command = BsonDocument(Seq(
"addUser" -> BsonString("dev"),
"pwd" -> BsonString("password"),
"roles" -> BsonArray(Seq(BsonString("readWrite")))
))
val observable = db.runCommand(command)
observable.subscribe(new CommandObserver[Document])
class CommandObserver[T] extends Observer[T] {
override def onNext(result: T): Unit = println(s"Success: $result")
override def onError(e: Throwable): Unit = println(s"Failure: $e")
override def onComplete(): Unit = println("Done")
}
这是我收到的输出:
失败:com.mongodb.MongoCommandException:命令失败并显示错误 -1:服务器localhost:27017上的'no such cmd:addUser'。完整的响应是{“ok”:0.0,“errmsg”:“没有这样的cmd:addUser”,“坏的cmd” :{“addUser”:“dev”,“pwd”:“”,“roles”:[“readWrite”]}}
我的代码出了什么问题?