문제

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