【问题标题】:find by regular expression with Casbah使用 Casbah 通过正则表达式查找
【发布时间】: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


    【解决方案1】:

    您已经很接近了,您只需将您的条件包装在 MongoDBObject() 中。

    我们不得不在很多地方提取 &lt;key&gt; -&gt; &lt;value&gt; 的隐式转换,因为它们很难正确捕获并且会破坏其他代码。

    他们可能会在 2.1 中回归。

    改为这样做:

    val coll = MongoConnection()("foo")("bar")
    for(x <- coll.find(MongoDBObject("name" -> ".*son$".r))) {
       // some operations...
    }
    

    【讨论】:

      【解决方案2】:

      对于添加 IGNORECASE 上述答案将无法通过在 Casbah Scala 中的正则表达式末尾附加“/i”来工作。 为此目的使用:

      val EmailPattern = Pattern.compile(companyName,Pattern.CASE_INSENSITIVE)
      val q = MongoDBObject("companyName" ->  EmailPattern)
      val result = MongoFactory.COLLECTION_NAME.findOne(q)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-10
        • 1970-01-01
        • 2015-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-29
        相关资源
        最近更新 更多