Frage

what's the best MongoDB driver for Play Framework 2.1?

I am trying ReactiveMongo at this moment but I cannot find good documentation in anywhere and I have my doubts about its future development.

Which driver is the most popular and supported?

Thanks,

GA

War es hilfreich?

Lösung

I didn't do any comparison so I wouldn't claim it's the best, but when I started with my current project there was only salat with its Play! plugin. It's quite well documented (see its github wiki) and under active development. I'd say it has production quality. If the documentation isn't enough for you there are samples of usage in the test suites in the repository.

Andere Tipps

We were sort of unsatisfied with the way Casbah works for deep objects or simple maps and no real case class mapping support so we rolled our own MongoDB Synchronous Scala driver on top of the legacy java driver which I would like to shamelessly plug here with an example on how to store and retrieve a map and a simple case class. The driver does not have a lot of magic and is easy to setup and features a simple BSON implementation which was inspired by the Play2 JSON impl.

Here is how to use it with some simple values:

val client = MongoClient("hostname", 27017)
val db = client("dbname")
val coll = db("collectionname")

coll.save(Bson.doc("_id" -> 1, "vals" -> Map("key1" -> "val1")))
val docOpt = coll.findOneById(1)  // => Option[BsonDoc]

for(doc <- docOpt)
  println(doc.as[Map[String, String]]("vals")("key1"))  // => prints "val1"

And with a case class it is some mapping needed but this was a design decision we made because we wanted to have the mapping completely customizable without understanding any real framework:

case class DnsRecord(host: String = "", ttl: Long = 0, otherProps: Map[String, String] = Map())

case object DnsRecord {
  implicit object DnsRecordToBsonElement extends ToBsonElement[DnsRecord] {
    def toBson(v: DnsRecord): BsonElement = DnsRecordToBsonDoc.toBson(v)
  }

  implicit object DnsRecordFromBsonElement extends FromBsonElement[DnsRecord] {
    def fromBson(v: BsonElement): DnsRecord = DnsRecordFromBsonDoc.fromBson(v.asInstanceOf[BsonDoc])
  }

  implicit object DnsRecordFromBsonDoc extends FromBsonDoc[DnsRecord] {
    def fromBson(d: BsonDoc): DnsRecord = DnsRecord(
      d[String]("host"),
      d[Long]("ttl"),
      d[Map[String, String]]("op")
    )
  }

  implicit object DnsRecordToBsonDoc extends ToBsonDoc[DnsRecord] {
    def toBson(m: DnsRecord): BsonDoc = Bson.doc(
      "host" -> m.host,
      "ttl" -> m.ttl,
      "op" -> m.otherProps
    )
  }
}

coll.save(DnsRecord("test.de", 4456, Map("p2" -> "val1")))
for (r <- coll.findAs[DnsRecord](Bson.doc("host" -> "test.de")))
  println(r.host)

You could try to use Moscale I implement this library with my colegues as part of another proprietary project. Library is used in production now. There is lack of documentation, but it extreamly usefull and very simple. You can look at tests, instead of documentation and there is a short example of simple usage.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top