【发布时间】:2011-02-03 15:07:41
【问题描述】:
Collection#find(/* HERE */)@如何使用正则表达式如:
val coll = MongoConnection()("foo")("bar")
for(x <- coll.find("name" -> ".*son$".r)) {
// some operations...
}
【问题讨论】:
标签: scala mongodb casbah mongodb-scala
Collection#find(/* HERE */)@如何使用正则表达式如:
val coll = MongoConnection()("foo")("bar")
for(x <- coll.find("name" -> ".*son$".r)) {
// some operations...
}
【问题讨论】:
标签: scala mongodb casbah mongodb-scala
您已经很接近了,您只需将您的条件包装在 MongoDBObject() 中。
我们不得不在很多地方提取 <key> -> <value> 的隐式转换,因为它们很难正确捕获并且会破坏其他代码。
他们可能会在 2.1 中回归。
改为这样做:
val coll = MongoConnection()("foo")("bar")
for(x <- coll.find(MongoDBObject("name" -> ".*son$".r))) {
// some operations...
}
【讨论】:
对于添加 IGNORECASE 上述答案将无法通过在 Casbah Scala 中的正则表达式末尾附加“/i”来工作。 为此目的使用:
val EmailPattern = Pattern.compile(companyName,Pattern.CASE_INSENSITIVE)
val q = MongoDBObject("companyName" -> EmailPattern)
val result = MongoFactory.COLLECTION_NAME.findOne(q)
【讨论】: