[英]mongodb casbah and list handling
I am having problems writing this function, which takes a string and returns a list of strings associated to it. 我在编写此函数时遇到问题,该函数需要一个字符串并返回与其关联的字符串列表。
(I'm expecting entries like {_id: ...., hash: "abcde", n: ["a","b","ijojoij"]} in mongodb) (我希望在mongodb中像{_id:....,hash:“ abcde”,n:[“ a”,“ b”,“ ijojoij”]}这样的条目)
def findByHash(hash: Hash) = {
val dbobj = mongoColl.findOne(MongoDBObject("hash" -> hash.hashStr))
val n = dbobj match {
case Some(doc: com.mongodb.casbah.Imports.DBObject) => {
doc("n") match {
case Some(n: com.mongodb.casbah.Imports.DBObject) => {
Some(List[String]() ++ n map { x => x.asInstanceOf[String] })
}
case _ => {
None // hash match but no n in object
}
}
}
case _ => {
None // no hash match
}
}
n
}
Is there anything wrong with the code? 代码有什么问题吗? Do you know how to correct it?
你知道如何纠正吗?
doc("n") returns AnyRef, so you should explicitly cast it to BasicDBList. doc(“ n”)返回AnyRef,因此您应将其显式转换为BasicDBList。
val n = doc("n").asInstanceOf[BasicDBList]
Some(List[String]() ++ n map { x => x.asInstanceOf[String] })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.