这是问题所在。我有mongos
连接到远程的本地实例mongod
。远程数据库使用基本密码认证。我正在尝试使用简单的 Scala 应用程序为特定集合设置 ChangeStream 观察程序。实际代码如下所示:
private val mongo = new MongoClient(
new ServerAddress("localhost", 27017),
MongoCredential.createCredential("username", "myDB", "password".toCharArray),
MongoClientOptions.builder().addServerListener(ServerStateListener).build()
)
private val collection = mongo
.getDatabase(DB)
.getCollection("someObjectsCollection")
private val ch = collection
.watch()
.fullDocument(FullDocument.UPDATE_LOOKUP)
.iterator()
它断线.fullDocument(FullDocument.UPDATE_LOOKUP)
告诉:
Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on myDB to execute command { aggregate: "someObjectsCollection", pipeline: [ { $changeStream: { fullDocument: "updateLookup" } } ], cursor: {}, $db: "myDB", $clusterTime: { clusterTime: Timestamp(1524064297, 2), ....
这很令人困惑,因为给定的用户凭据mongo shell
在 remote DB 和 local 上都有效mongos
。此外,我尝试在该应用程序中使用集合执行一些其他操作(如collection.count()
)并且它有效!当我尝试设置观察者时出现问题。