I have a document in MongoDB that looks like this:
{"_id":"asdf", "data":[
{"a":"1","b":"2"},
{"a":"3","b":"4"},
{"a":"5","b":"6"},
]}
I would like to query that object using Scala, and convert the entries in "data" into a list of case classes. After a few hours' work, I've yet to come up with something that even compiles. Can someone point me to a tutorial with this information? This tutorial hasn't been any help. I've tried every combination of nested maps, fors, foreaches, casts, and pattern matching that I can come up with.
Edit: My super-ugly but now seemingly working code is now this:
def getData(source_id:String) = {
val source = collection.findOne(MongoDBObject("_id" -> source_id)).get
val data = source.get("data").asInstanceOf[BasicDBList]
var ret:List[Data] = List()
val it = presses.iterator
while(it.hasNext) {
val item = it.next.asInstanceOf[BasicDBObject]
ret = Data(
item.get("a").asInstanceOf[String],
item.get("b").asInstanceOf[String]
) :: ret
}
ret
}
Please, someone tell me there's a better way.