Вопрос

one thing I came across is the boilerplate code when querying in a mongodb :

def findPagesWithGreaterId(pageid: String, limit: Int): List[Page] =
    findAsListSortedLimit(MongoDBObject("_id" -> MongoDBObject("$gt" -> new ObjectId(pageid))), MongoDBObject("_id" -> 1), limit)

Is there a nice way of shortening the queries like in the above example?

(implicits are not handy i guess)

is there a common approach to it?

thanks in advance,

Stefan

Это было полезно?

Решение

You can define a type alias just to shorten class names:

type DBO = MongoDBObject

or in a similar way rename a class on import:

import com.mongodb.casbah.commons.{MongoDBObject => DBO}

I don't like these solutions much. A better thing to do would be to use Rogue. It not just reads better, it's actually much more typesafe. The only downside is that you have to define your DB classes first before you can query, but once you do, your query would look like:

Pages.where(_.id gt new ObjectId(pageid)).orderAsc(_.id).limit(1)

or something like that :).

Check here for more examples.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top