【问题标题】:Grails MongoDB FindAll does not work for special charactersGrails MongoDB FindAll 不适用于特殊字符
【发布时间】:2014-07-09 15:32:47
【问题描述】:

我一直在尝试将搜索功能添加到我的 grails 项目中

这是我的域类

class Resource {
    String mimeType
    String resourceUrl
    String website
    static constraints = {
    }
}

这是我的控制器

class ResourceController {
def report()
    {
    def website = params.website
    println "inside report " + website
    def res = Resource.findAllByWebsite(website)
    int noOfResources = res.size()
    println noOfResources
    }

此代码适用于没有任何特殊字符(如 www.google.com)的网站值,但出于某种原因,只要网站值中有任何特殊字符,如 ?或任何东西,它不会从数据库中获取任何东西。请建议我另一种方法。

我也试过这个:def res = Resource.findAllByWebsiteLike(website),但也没有用。

【问题讨论】:

  • 您是否尝试转义特殊字符(即使用“\?”而不是“?”等...)?
  • 我做到了。但是存储在数据库中的网站的值是没有任何转义字符的。它没有用。我正在寻找一种允许特殊字符的 findAll 的替代方法

标签: mongodb grails grails-orm findall


【解决方案1】:

您是否尝试过使用正则表达式搜索?

如:

def searchMap
searchMap.put(fieldSearch, [ '$regex' : website, '$options': 'i'] )
def res = Resource.collection.find(searchMap)

【讨论】:

    猜你喜欢
    • 2014-08-09
    • 2016-03-03
    • 1970-01-01
    • 2016-09-13
    • 2019-09-17
    • 1970-01-01
    • 2020-04-02
    • 2021-09-05
    • 2015-02-27
    相关资源
    最近更新 更多