0
我無法使用新的scala驅動程序在mongoDB中檢索文檔。mongodb scala驅動程序 - 無法檢索文檔
這裏我的代碼:
def retrieveDocument(id: Int,
collectionName: String,
databaseName: String,
url: String): Option[Document] = {
var res: Option[Document] = None
getMongoCollectionImmutable(collectionName, databaseName, url)
.find(Filters.equal("_id", id))
.first().subscribe(
(doc: Document) => res = Some(doc),
(e: Throwable) => throw e,
() =>())
res
}
def getMongoCollectionImmutable(collectionName: String,
databaseName: String,
url: String = DEFAULT_URL): MongoCollection[ImmuDoc] = {
db match {
case None =>
getMongoDatabase(databaseName, url).getCollection(collectionName)
case Some(db) =>
db.client.getDatabase(databaseName).getCollection(collectionName)
}
def getMongoDatabase(name: String, url: String = DEFAULT_URL): MongoDatabase = {
db match {
case None =>
db = Some(new _Database(url))
getMongoDatabase(name)
case Some(db) =>
db.client.getDatabase(name)
}
def retrieve(id: Int): Try[User] = {
try {
val docOption = Database.retrieveDocument(id, USER_COLLECTION, DATABASE_NAME, DEFAULT_URL)
docOption match {
case None => Failure(new Exception(s"Unable to retrieve a user with id ${id}"))
case Some(doc) => Try(User(doc))
}
} catch {
case e: Throwable => Failure(e)
}
}
這裏是驅動程序的(顯著)日誌:
19:16:24.334 DEBUG cluster - Updating cluster description to {type=STANDALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=0.7 ms, state=CONNECTED}]
19:16:24.366 INFO connection - Opened connection [connectionId{localValue:2, serverValue:90}] to localhost:27017
19:16:24.377 DEBUG query - Asynchronously sending query of namespace jobless.user on connection [connectionId{localValue:2, serverValue:90}] to server localhost:27017
19:16:24.381 DEBUG query - Query results received 1 documents with cursor null
這裏我的測試輸出
Run starting. Expected test count is: 1
UserTest:
User Document((_id,BsonInt32{value=1}), (firstname,BsonString{value='user1'}), (lastname,BsonString{value='last1'}), (encryptedPass,BsonString{value='pass'}), (cvListPath,BsonArray{values=[{ "name" : "path1", "path" : "name1" }, { "name" : "path2", "path" : "name2" }]}), (motivationLettersPath,BsonArray{values=[{ "name" : "path1", "path" : "name1" }, { "name" : "path2", "path" : "name2" }]}))
- retrieve from DB Failure(java.lang.Exception: Unable to retrieve a user with id 1) *** FAILED ***
java.lang.Exception: Unable to retrieve a user with id 1 (UserTest.scala:31)
但!通過使用wireshark,我可以看到正確的數據庫查詢並返回文檔! (我檢查使用蒙戈linux命令進行查詢的,它是非常相同)
Frame 1262: 123 bytes on wire (984 bits), 123 bytes captured (984 bits) on interface 0
Linux cooked capture
Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
Transmission Control Protocol, Src Port: 42714, Dst Port: 27017, Seq: 438, Ack: 1272, Len: 55
Mongo Wire Protocol
Message Length: 55
Request ID: 0x00000008 (8)
Response To: 0x00000000 (0)
OpCode: Query (2004)
Query Flags
fullCollectionName: xxx.user
Number To Skip: 0
Number to Return: -1
Query
Document length: 14
Elements
Element: _id
Type: Int32 (0x10)
Value: 1
和數據庫的響應也是正確的。
我在做什麼錯?