开发者

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)

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
                    }
                }
            }
            cas开发者_JAVA百科e _ => {
                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.

val n = doc("n").asInstanceOf[BasicDBList]
Some(List[String]() ++ n map { x => x.asInstanceOf[String] })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜