【问题标题】:I get `""` (exact phrase) in mongodb query我在 mongodb 查询中得到 `""` (确切的短语)
【发布时间】:2019-07-07 03:48:55
【问题描述】:

我想要一个不区分单词和不区分大小写的查询。我使用/.*#{params[:query]}*./i 创建了以下查询:

def set_query
  @query = params[:query]
end

def set_documents
  @documents = @folder ? @folder.documents : current_user.documents
  @documents = (params[:query] ? @documents.where(
    :$text => {:$search => (/.*#{params[:query]}*./i).to_s}
  ) : @documents).page(params[:page])
end

但是当我搜索时,它是单词敏感的:rehabilitation 会给我标题中有rehabilitation 的文档,但如果我用rehab 搜索,它不会给我任何东西。

我看到我在搜索词中得到了"",这意味着在 mongodb 中是 exact phrase,但是我知道为什么我会得到这些 "" 以及如何删除它们。

"$text"=>{"$search"=>"(?i-mx:.*rehab*.)"},

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails mongodb mongoid


    【解决方案1】:

    您能否在不使用 to_s 的情况下尝试相同的查询。

    def set_documents
    @documents = @folder ? @folder.documents : current_user.documents
    @documents = (params[:query] ? @documents.where(
      :$text => {:$search => (/.*#{params[:query]}*./i)}
    ) : @documents).page(params[:page])
    end
    

    【讨论】:

    • 我必须输入to_s,因为$search 需要string。错误:$search" had the wrong type. Expected string, found regex (14)
    猜你喜欢
    • 1970-01-01
    • 2014-02-05
    • 2015-10-19
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    相关资源
    最近更新 更多