卡斯巴和Rogue for MongoDB - 查询功能

发布于 2024-11-06 12:58:20 字数 292 浏览 0 评论 0原文

我目前正在使用 Casbah 和 MongoDB 来实现 Web 服务。到目前为止我没有遇到任何问题。我也在使用 Scala。

然而,我只是想知道是否有比 Casbah 更好的东西来执行大量 find/findOne 类型查询。

我遇到了 Rogue,一种基于 Scala 的类型安全 DSL,据说可以使查询更容易、更具可读性。

所以,我想知道转向 Rogue 是否有用,以便随着 Web 服务项目变得更大、更复杂,拥有 Rogue 对查询的支持可能会有所帮助?

只是想知道我是否应该继续或转向更好的事情。

I'm currently using Casbah with MongoDB for implementing web service. I am having no problems with it so far. I am using Scala as well.

However, I was just curious to know if there is something better than Casbah for doing a lot of find/findOne type queries.

I came across Rogue, a type-safe Scala based DSL that says would make querying easier and more readable.

So, I wanted to know if it would be useful to shift to Rogue, so that as the web service project gets bigger and more complex, it might help to have Rogue's support for querying?

Just wanted to find out if I should continue or shift to something better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2024-11-13 12:58:20

目前,Rogue 仅适用于 Lift 的 MongoDB-Record 系统。

它提供完整类型安全性的部分原因是它为 Lift-Record 使用了强大的、定义良好的对象结构。由于您拥有完整的结构,因此执行“自由形式”查询的能力要少得多。这就是我在最近的 Scala 网络研讨会上所做的 Lift-Record + Rogue 演示的意思。 Lift & 中的一些内容发生了变化。由于我这样做了,所以代码可能有点过时,但仍然具有代表性。这是 MongoDB 的 Lift-Record 模型:

object LiftRecordDemo extends Application {
  // We'll use enums for Type and Subtype
  object EventType extends Enumeration {
    type EventType = Value
    val Conference, Webinar = Value
  }

  object EventSubType extends Enumeration {
    type EventSubType = Value
    val FullDay = Value("Full Day")
    val HalfDay = Value("Half Day")
  }



  class MongoEvent extends MongoRecord[MongoEvent] with MongoId[MongoEvent] {
    def meta = MongoEvent

    object name extends StringField(this, 255)
    object eventType extends EnumField(this, EventType) 
    object eventSubType extends OptionalEnumField(this, EventSubType)
    object location extends JsonObjectField[MongoEvent, EventLocation](this, EventLocation)  {
      def defaultValue = EventLocation(None, None, None, None, None, None, None)
    }

    object hashtag extends OptionalStringField(this, 32)
    object language extends OptionalStringField(this, 32)
    object date extends JsonObjectField[MongoEvent, EventDate](this, EventDate) {
      def defaultValue = EventDate(new DateTime, None)
    }

    object url extends OptionalStringField(this, 255)
    object presenter extends OptionalStringField(this, 255)

  }

  object MongoEvent extends MongoEvent with MongoMetaRecord[MongoEvent] {
    override def collectionName = "mongoEvents"
    override def formats = super.formats + new EnumSerializer(EventType) + new EnumSerializer(EventSubType)
  }


  case class EventLocation(val venueName: Option[String], val url: Option[String], val address: Option[String], val city: Option[String], val state: Option[String], val zip: Option[String], val country: Option[String]) extends JsonObject[EventLocation] {
    def meta = EventLocation
  }

  object EventLocation extends JsonObjectMeta[EventLocation]

  case class EventDate(start: DateTime, end: Option[DateTime]) extends JsonObject[EventDate] {
    def meta = EventDate
  }

  object EventDate extends JsonObjectMeta[EventDate]
}

如您所见,您需要提前定义 MongoDB 数据模型,以获得强类型、安全查询的优势……Rogue 在编译时强制执行其中大部分内容。以下是针对此模型的一些 Rogue 示例:

  // Tell Lift about our DB
  val mongoAddr = MongoAddress(MongoHost("127.0.0.1", 27017), "scalaWebinar")

  MongoDB.defineDb(DefaultMongoIdentifier, mongoAddr)

  // Rogue gives us a saner approach, although still hobbled by some
  // of Lift-MongoDB-Record's limits on embedded docs

  val q = MongoEvent where (_.eventType eqs EventType.Webinar)

  println("Rogue created a Query '%s'\n\n".format(q))

  for (x <- MongoEvent where (_.eventType eqs EventType.Webinar)) {
    println("Name: %s Presenter: %s\n".format(x.name, x.presenter))
  }

  // Rogue can also do sorting for you, which is useful

  println("\n\n\n")

  for (x <- MongoEvent where (_.eventType eqs EventType.Conference) 
                       orderAsc(_.language) andDesc(_.name)) { 
    println("Name: %s Language: %s\n".format(x.name, x.language))
  }
  val start = new DateTime(2011, 2, 1, 0, 0, 0, 0)
  val end = new DateTime(2011, 3, 1, 0, 0, 0, 0)

    /** The following would be nice but unfortunately, 
      doesn't work because of lift's current embedded doc
      implementation
    */
  //val dateQ = MongoEvent where (_.date.start after start) 
                           //and (_.date.end before end)

请注意,我并不是说 Rogue 和 Lift-Record 并不出色,只是它们基于严格定义的编译时数据模型工作。

如果您想使用与 Casbah 类似的上下文,我们确实有一个内置 DSL,旨在尽可能模仿 MongoDB 的内置查询模型。它适用于任何任意底层模型,但在可能的情况下做了很多工作来强制执行类型安全级别。下面是 Casbah 查询的示例(由于来自同一演示文稿而略有过时):

  // What about querying?  Lets find all the non-US events

  for (x <- mongo.find(MongoDBObject("location.country" -> 
                        MongoDBObject("$ne" -> "USA")))) println(x)

  /* There's a problem here: We got back the Webinars too because 
     They don't have a country at all, so they aren't "USA"
    */
  println("\n\nTesting for existence of Location.Country:")

  for (x <- mongo.find(MongoDBObject("location.country" -> MongoDBObject(
                       "$ne" -> "USA",
                       "$exists" -> true 
                      )))) println(x)

  // This is getting a bit unwieldy.  Thankfully, Casbah offers a DSL
  val q = $or ("location.country" -> "USA", "location.country" -> "Japan")

  println("\n Created a DBObject: %s".format(q))

  println("\n Querying using DSL Object...")

  for (x <- mongo.find(q)) println(x)

  // It's possible to construct more complex queries too.

  // Lets find everything in February

  println("\n February Events...")
  val start = new DateTime(2011, 2, 1, 0, 0, 0, 0)
  val end = new DateTime(2011, 3, 1, 0, 0, 0, 0)
  val dateQ = "date.start" $gte start $lt end 

  println("\n Date Query: %s".format(dateQ))

  for (x <- mongo.find(dateQ, MongoDBObject("name" -> true, "date" -> true))) println(x)

值得注意的是,我们正在针对自由格式模型进行查询,但使用 DSL 运算符而不是嵌套的 MongoDB 定义。有很多奇妙的运算符映射,一直到 $type 运算符,以使用类清单测试类型以确保编译时安全:(

"Casbah's $type operator" should {
  "Accept raw Byte indicators (e.g. from org.bson.BSON)" in {
    // Don't need to test every value here since it's just a byte
    val typeOper = "foo" $type org.bson.BSON.NUMBER_LONG
    typeOper must notBeNull
    typeOper.toString must notBeNull
    typeOper must haveSuperClass[DBObject]
    typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NUMBER_LONG))
  }

  "Accept manifested Type arguments" in {
    "Doubles" in {
      val typeOper = "foo".$type[Double]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NUMBER))
    }
    "Strings" in {
      val typeOper = "foo".$type[String]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.STRING))
    }
    "Object" in {
      "via BSONObject" in {
        val typeOper = "foo".$type[org.bson.BSONObject]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.OBJECT))
      }
      "via DBObject" in {
        val typeOper = "foo".$type[DBObject]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.OBJECT))
      }
    }
    "Array" in {
      "via BasicDBList" in {
        val typeOper = "foo".$type[BasicDBList]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.ARRAY))
      }
      "via BasicBSONList" in {
        val typeOper = "foo".$type[org.bson.types.BasicBSONList]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.ARRAY))
      }
    }
    "OID" in {
      val typeOper = "foo".$type[ObjectId]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.OID))
    }
    "Boolean" in {
      val typeOper = "foo".$type[Boolean]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.BOOLEAN))
    }
    "Date" in {
      "via JDKDate" in {
        val typeOper = "foo".$type[java.util.Date]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.DATE))
      }
      "via Joda DateTime" in {
        val typeOper = "foo".$type[org.joda.time.DateTime]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.DATE))
      }
    }
    "None (null)" in {
      // For some reason you can't use NONE 
      val typeOper = "foo".$type[Option[Nothing]]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NULL))
    }
    "Regex" in {
      "Scala Regex" in {
        val typeOper = "foo".$type[scala.util.matching.Regex]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.REGEX))
      }
    }
    "Symbol" in {
      val typeOper = "foo".$type[Symbol]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.SYMBOL))
    }
    "Number (integer)" in {
      val typeOper = "foo".$type[Int]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NUMBER_INT))
    }
    "Number (Long)" in {
      val typeOper = "foo".$type[Long]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NUMBER_LONG))
    }
    "Timestamp" in {
      val typeOper = "foo".$type[java.sql.Timestamp]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.TIMESTAMP))
    }
    "Binary" in {
      val typeOper = "foo".$type[Array[Byte]]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.BINARY))
    }

  }

注意:您需要将 casbah-query 包及其相关导入导入到您的代码中,或者使用默认“casbah”从预模块化导入)。 Casbah 规范目前已全面覆盖每个 DSL 运营商;目前文档滞后,但规范可以很好地介绍其用法。请注意,Casbah 中有两种类型的运算符,就像 MongoDB 中一样。

裸字运算符,其中语句的最左边部分是$运算符。示例有 $set$rename 等。请参阅 裸字运算符规范了解更多信息。

“核心”运算符是存在于语句右侧的运算符,例如$type。请参阅核心运算符规范 了解更多。

我知道这是一个相当详细的答案,但我想确保您了解您的选择以及两种解决方案的局限性。 Casbah 将为您提供一个与 MongoDB 紧密映射的 DSL,并消除了一些语法缺陷(请记住,Casbah 在 DBObject 上提供了一个 getAs[T] 方法来请求来自DBObject的值作为特定类型,人们经常忽略);许多用户在寻求某些东西来执行内置功能之前并不知道 DSL 的存在。但是,在某些人看来,Casbah 的 Query DSL 也看起来有点“粗鲁”......作为它的作者我更喜欢它的简单和优雅,因为我只需要记住用于查询的 **MONGODB* 语法,而不需要记住 MongoDB 和其他 DSL。它也基于自由形式查询,并且不提供相同的结构化、编译时类型安全和查询。意识到 Rogue 所做的设施。

但相比之下,Rogue 还需要一个完全定义的记录模型,这并不适合每个应用程序。

然而,如果两种产品都不能很好地满足您的需求,那么我很想知道两种产品可以在哪些方面进行改进。

At the moment, Rogue only works against Lift's MongoDB-Record system.

Part of the reason it offers full type safety is that it uses a strong, well defined object structure for Lift-Record. You have a lot less ability to do 'free form' queries as you have a full structure in place for it. Here's what I mean by way of a demo of Lift-Record + Rogue I did for a recent Scala Webinar. Some stuff has changed in Lift & Rogue since I did this so code might be slightly out of date but is still representative. This is the Lift-Record model for MongoDB:

object LiftRecordDemo extends Application {
  // We'll use enums for Type and Subtype
  object EventType extends Enumeration {
    type EventType = Value
    val Conference, Webinar = Value
  }

  object EventSubType extends Enumeration {
    type EventSubType = Value
    val FullDay = Value("Full Day")
    val HalfDay = Value("Half Day")
  }



  class MongoEvent extends MongoRecord[MongoEvent] with MongoId[MongoEvent] {
    def meta = MongoEvent

    object name extends StringField(this, 255)
    object eventType extends EnumField(this, EventType) 
    object eventSubType extends OptionalEnumField(this, EventSubType)
    object location extends JsonObjectField[MongoEvent, EventLocation](this, EventLocation)  {
      def defaultValue = EventLocation(None, None, None, None, None, None, None)
    }

    object hashtag extends OptionalStringField(this, 32)
    object language extends OptionalStringField(this, 32)
    object date extends JsonObjectField[MongoEvent, EventDate](this, EventDate) {
      def defaultValue = EventDate(new DateTime, None)
    }

    object url extends OptionalStringField(this, 255)
    object presenter extends OptionalStringField(this, 255)

  }

  object MongoEvent extends MongoEvent with MongoMetaRecord[MongoEvent] {
    override def collectionName = "mongoEvents"
    override def formats = super.formats + new EnumSerializer(EventType) + new EnumSerializer(EventSubType)
  }


  case class EventLocation(val venueName: Option[String], val url: Option[String], val address: Option[String], val city: Option[String], val state: Option[String], val zip: Option[String], val country: Option[String]) extends JsonObject[EventLocation] {
    def meta = EventLocation
  }

  object EventLocation extends JsonObjectMeta[EventLocation]

  case class EventDate(start: DateTime, end: Option[DateTime]) extends JsonObject[EventDate] {
    def meta = EventDate
  }

  object EventDate extends JsonObjectMeta[EventDate]
}

As you can see, you are required to define your MongoDB Data model ahead of time in order to gain the benefit of strongly typed, safe queries... Rogue enforces most of it at compile time. Here are some Rogue examples against this model:

  // Tell Lift about our DB
  val mongoAddr = MongoAddress(MongoHost("127.0.0.1", 27017), "scalaWebinar")

  MongoDB.defineDb(DefaultMongoIdentifier, mongoAddr)

  // Rogue gives us a saner approach, although still hobbled by some
  // of Lift-MongoDB-Record's limits on embedded docs

  val q = MongoEvent where (_.eventType eqs EventType.Webinar)

  println("Rogue created a Query '%s'\n\n".format(q))

  for (x <- MongoEvent where (_.eventType eqs EventType.Webinar)) {
    println("Name: %s Presenter: %s\n".format(x.name, x.presenter))
  }

  // Rogue can also do sorting for you, which is useful

  println("\n\n\n")

  for (x <- MongoEvent where (_.eventType eqs EventType.Conference) 
                       orderAsc(_.language) andDesc(_.name)) { 
    println("Name: %s Language: %s\n".format(x.name, x.language))
  }
  val start = new DateTime(2011, 2, 1, 0, 0, 0, 0)
  val end = new DateTime(2011, 3, 1, 0, 0, 0, 0)

    /** The following would be nice but unfortunately, 
      doesn't work because of lift's current embedded doc
      implementation
    */
  //val dateQ = MongoEvent where (_.date.start after start) 
                           //and (_.date.end before end)

Notice I am not saying Rogue and Lift-Record aren't awesome, just that they work off of a strongly defined compile time data model.

If instead you'd like to use a similar context with Casbah, we do have a builtin DSL which is designed to mimic MongoDB's inbuilt Query model as closely as possible. It works against any arbitrary underlying model, but does a LOT to enforce levels of type safety where possible. Here is a (slightly dated as its from the same presentation) example of Casbah's Querying:

  // What about querying?  Lets find all the non-US events

  for (x <- mongo.find(MongoDBObject("location.country" -> 
                        MongoDBObject("$ne" -> "USA")))) println(x)

  /* There's a problem here: We got back the Webinars too because 
     They don't have a country at all, so they aren't "USA"
    */
  println("\n\nTesting for existence of Location.Country:")

  for (x <- mongo.find(MongoDBObject("location.country" -> MongoDBObject(
                       "$ne" -> "USA",
                       "$exists" -> true 
                      )))) println(x)

  // This is getting a bit unwieldy.  Thankfully, Casbah offers a DSL
  val q = $or ("location.country" -> "USA", "location.country" -> "Japan")

  println("\n Created a DBObject: %s".format(q))

  println("\n Querying using DSL Object...")

  for (x <- mongo.find(q)) println(x)

  // It's possible to construct more complex queries too.

  // Lets find everything in February

  println("\n February Events...")
  val start = new DateTime(2011, 2, 1, 0, 0, 0, 0)
  val end = new DateTime(2011, 3, 1, 0, 0, 0, 0)
  val dateQ = "date.start" $gte start $lt end 

  println("\n Date Query: %s".format(dateQ))

  for (x <- mongo.find(dateQ, MongoDBObject("name" -> true, "date" -> true))) println(x)

Notably we are querying against a free form model but using DSL operators instead of nested MongoDB definitions. There are lots of fantastic mappings of operators, right down to the $type operator to test a type using class manifests for compiletime safety:

"Casbah's $type operator" should {
  "Accept raw Byte indicators (e.g. from org.bson.BSON)" in {
    // Don't need to test every value here since it's just a byte
    val typeOper = "foo" $type org.bson.BSON.NUMBER_LONG
    typeOper must notBeNull
    typeOper.toString must notBeNull
    typeOper must haveSuperClass[DBObject]
    typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NUMBER_LONG))
  }

  "Accept manifested Type arguments" in {
    "Doubles" in {
      val typeOper = "foo".$type[Double]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NUMBER))
    }
    "Strings" in {
      val typeOper = "foo".$type[String]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.STRING))
    }
    "Object" in {
      "via BSONObject" in {
        val typeOper = "foo".$type[org.bson.BSONObject]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.OBJECT))
      }
      "via DBObject" in {
        val typeOper = "foo".$type[DBObject]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.OBJECT))
      }
    }
    "Array" in {
      "via BasicDBList" in {
        val typeOper = "foo".$type[BasicDBList]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.ARRAY))
      }
      "via BasicBSONList" in {
        val typeOper = "foo".$type[org.bson.types.BasicBSONList]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.ARRAY))
      }
    }
    "OID" in {
      val typeOper = "foo".$type[ObjectId]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.OID))
    }
    "Boolean" in {
      val typeOper = "foo".$type[Boolean]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.BOOLEAN))
    }
    "Date" in {
      "via JDKDate" in {
        val typeOper = "foo".$type[java.util.Date]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.DATE))
      }
      "via Joda DateTime" in {
        val typeOper = "foo".$type[org.joda.time.DateTime]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.DATE))
      }
    }
    "None (null)" in {
      // For some reason you can't use NONE 
      val typeOper = "foo".$type[Option[Nothing]]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NULL))
    }
    "Regex" in {
      "Scala Regex" in {
        val typeOper = "foo".$type[scala.util.matching.Regex]
        typeOper must notBeNull
        typeOper.toString must notBeNull
        typeOper must haveSuperClass[DBObject]
        typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.REGEX))
      }
    }
    "Symbol" in {
      val typeOper = "foo".$type[Symbol]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.SYMBOL))
    }
    "Number (integer)" in {
      val typeOper = "foo".$type[Int]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NUMBER_INT))
    }
    "Number (Long)" in {
      val typeOper = "foo".$type[Long]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.NUMBER_LONG))
    }
    "Timestamp" in {
      val typeOper = "foo".$type[java.sql.Timestamp]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.TIMESTAMP))
    }
    "Binary" in {
      val typeOper = "foo".$type[Array[Byte]]
      typeOper must notBeNull
      typeOper.toString must notBeNull
      typeOper must haveSuperClass[DBObject]
      typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.BINARY))
    }

  }

(Note: You need to either import the casbah-query package and its related imports to your code or use the default 'casbah' import from pre-modulisation). The Specs for Casbah currently have full coverage for every DSL operator; the docs lag behind at the moment but the Specs serve as a great introduction to their usage. Note there are two kinds of operators in Casbah, just like in MongoDB.

Bareword Operators, in which the leftmost part of the statement is the $ Operator. Examples of this are $set, $rename, etc. See the Specs for Bareword Operators for more.

"Core" Operators, are operators that exist on the right hand side of the statement, such as $type. See the Specs for Core Operators for more.

I know this is a rather detailed answer but I wanted to make sure that you understand your options, as well as the limitations of both solutions. Casbah will give you a DSL that maps closely to MongoDB and removes some of the syntactic cruft (Keep in mind also Casbah provides a getAs[T] method on DBObject to ask for a value from the DBObject as a specific type, which people often overlook); many users are unaware the DSL exists before they go seeking something to do what is built in. However, Casbah's Query DSL also looks a bit "crufty" in some peoples opinions... as the author of it I prefer it's simplicity and elegance as I need only remember the **MONGODB* syntax for querying, and not both MongoDB and another DSL. It is also based on free form querying and doesn't provide the same structured, compile time type safe & aware facilities that Rogue does.

By contrast though, Rogue also requires a fully defined Record model against it, which doesn't fit into every application.

I'd love to hear however where things can be improved on either product if there's a "middle ground" for your needs which either product doesn't properly meet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文