简体   繁体   English

Play Framework 2.1的最佳mongodb驱动程序

[英]Best mongodb driver for Play Framework 2.1

what's the best MongoDB driver for Play Framework 2.1? 什么是Play Framework 2.1最好的MongoDB驱动程序?

I am trying ReactiveMongo at this moment but I cannot find good documentation in anywhere and I have my doubts about its future development. 我现在正在尝试ReactiveMongo,但我无法在任何地方找到好的文档,我怀疑它的未来发展。

Which driver is the most popular and supported? 哪个驱动程序最受欢迎和支持?

Thanks, 谢谢,

GA GA

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. 它有很好的文档记录(参见其github wiki)并且正在积极开发中。 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. 如果文档不够,则存储库中的测试套件中会有使用示例。

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. 我们对Casbah用于深层对象或简单映射的方式以及没有真正的案例类映射支持感到不满意所以我们在遗留的java驱动程序之上推出了我们自己的MongoDB Synchronous Scala驱动程序,我想通过示例无耻地插入这里关于如何存储和检索地图以及简单的案例类。 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 . 该驱动程序没有太大的魔力,易于设置,并具有简单的BSON实现,其灵感来自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. 你可以尝试使用Moscale我用我的colegues实现这个库作为另一个专有项目的一部分。 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. 您可以查看测试而不是文档,并且有一个简单用法的简短示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM