【问题标题】:How to OR in mongo $test search with Phrases如何使用短语在 mongodb $text 搜索中进行 OR
【发布时间】:2017-04-21 04:18:04
【问题描述】:

docs为例:

"\"ssl 证书\"授权密钥"

$text 运算符搜索短语“ssl certificate”和(“authority”或“key”或“ssl”或“c​​ertificate”)。

有没有办法搜索“ssl证书”或“授权”或“密钥”?

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    不幸的是,涉及任何短语匹配的文本搜索将始终仅从具有该短语的文档开始。您也许可以通过使用聚合框架来解决这个问题?如果您的文本索引在“textField”字段上,您可以这样做:

    db.collection.aggregate([
      {$match: {$text: {$search: "ssl certificate authority key"}}},
      {$match: {$or: [
        {textField: {$regex: /ssl certificate/}},
        {textField: {$regex: /authority/}},
        {textField: {$regex: /key/}}
      ]}}
    ])
    

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 2012-07-15
      • 2012-10-26
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多