Scala 卡斯巴;MongoDB的Rogue-查询功能
我目前正在使用Casbah和MongoDB来实现web服务。到目前为止,我没有任何问题。我也在使用Scala 然而,我只是想知道是否有比Casbah更好的方法来执行大量find/findOne类型查询 我遇到了Rogue,一种类型安全的基于Scala的DSL,它说这将使查询更容易、更可读 所以,我想知道转向Rogue是否有用,这样随着web服务项目变得越来越大、越来越复杂,Rogue对查询的支持可能会有所帮助Scala 卡斯巴;MongoDB的Rogue-查询功能,scala,mongodb,casbah,Scala,Mongodb,Casbah,我目前正在使用Casbah和MongoDB来实现web服务。到目前为止,我没有任何问题。我也在使用Scala 然而,我只是想知道是否有比Casbah更好的方法来执行大量find/findOne类型查询 我遇到了Rogue,一种类型安全的基于Scala的DSL,它说这将使查询更容易、更可读 所以,我想知道转向Rogue是否有用,这样随着web服务项目变得越来越大、越来越复杂,Rogue对查询的支持可能会有所帮助 我只是想知道我是应该继续还是转向更好的方向 目前,Rogue只对系统有效 它提供全类型
我只是想知道我是应该继续还是转向更好的方向 目前,Rogue只对系统有效 它提供全类型安全的部分原因是它使用了一个强大的、定义良好的对象结构来记录提升。你做“自由形式”查询的能力要小得多,因为你有一个完整的结构。以下是我在最近的Scala网络研讨会上做的Lift Record+Rogue演示的意思。自从我这么做之后,Lift&Rogue中的一些内容发生了变化,所以代码可能有点过时,但仍然具有代表性。这是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]
}
如您所见,您需要提前定义MongoDB数据模型,以获得强类型、安全查询的好处。。。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)
(注意:您需要将casbah查询包及其相关导入导入到您的代码中,或者使用来自预模块化的默认“casbah”导入)。Casbah的规范目前已覆盖所有DSL运营商;文档目前还比较落后,但是规范对它们的使用做了很好的介绍。注意Casbah中有两种操作符,就像MongoDB中一样
裸字运算符,其中语句最左边的部分是$Operator。例如$set
,$rename
等。有关更多信息,请参阅
“核心”运算符是存在于语句右侧的运算符,例如$type
。更多信息,请参阅
我知道这是一个相当详细的答案,但我想确保您了解您的选择,以及这两种解决方案的局限性。Casbah将为您提供一个与MongoDB紧密映射的DSL,并删除一些语法错误(请记住Casbah在
DBObject
上提供了一个getAs[T]
方法,以从DBObject
中请求一个特定类型的值,这是人们经常忽略的);许多用户在寻找内置功能之前,并不知道DSL的存在。然而,Casbah的查询DSL在一些人看来也有点“粗糙”。。。作为它的作者,我更喜欢它的简单和优雅,因为我只需要记住**MONGODB*查询语法,而不是MONGODB和另一个DSL。它还基于自由形式查询,不提供与Rogue相同的结构化、编译时类型安全和感知功能
相比之下,Rogue还需要一个完全定义的记录模型来对抗它,这并不适合每个应用程序
但是,如果有一个“中间地带”满足您的需求,而这两种产品都不能很好地满足您的需求,我很想知道这两种产品在哪些方面可以改进。“我更喜欢它的简单和优雅,因为我只需要记住用于查询的*MONGODB语法,而不需要同时记住MONGODB和其他DSL。”-这可能会让我暂时留在卡斯巴。我不想为编译时类型安全性增加更多的复杂性,我想我只是想确保Rogue可以做的所有事情都可以用Casbah自己来完成,而不会让它太难实现。到目前为止,CASBAH对我来说是相当好的,我可能不想使用不同的数据模型。你也可以考虑添加[Sala](它将对象映射到CasBah),并支持Casbh的查询。
// 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)
"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))
}
}