Reading the results of a Casbah MongoDB query

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.

Answers


As you are using case classes anyway, the easiest solution is to just use salat – it will automatically serialize/deserialize to and from a mongo connection with very little boilerplate.

A minor point, but in your code you should be able to simply map across the DBObject holding structure rather than manually mutate the ret variable:

val ret = presses.map { item => Data(…) }

you may need to call .toList if you really want a List (though you may only need Seq or Iterable)


Need Your Help

Get a pixels/units relation with opengl es

android opengl-es view pixels units-of-measurement

I am introducing me on Opengl ES and I am able draw textures with vertex an move it on the screen.