【问题标题】:Text Search in MongodDB [duplicate]MongodB中的文本搜索[重复]
【发布时间】:2018-04-23 04:48:29
【问题描述】:

考虑以下代码:

Transactions: [
               {name: 123, content: "Starbucks Coffee"},
               {name: 456, content: "Peets Coffee"},
               {name: 789, content: "Amazon gift card"},
               {name: 646, content: "amazon gift card"},
               {name: 779, content: "Acme Corp deposit cheque"},
               {name: 606, content: "acme corp deposit cheque"},
               {name: 879, content: "Dunkin-Donuts"},
               {name: 656, content: "Dunkin Donuts"}
              ];

我需要编写一个 MongoDB 框架,它执行以下操作:

案例 1.)

输入:“Acme Corp”或“acme Corp”或“Acme corp”或“acme corp”

预期输出:

       [
        {name: 779, content: "Acme Corp deposit cheque"},
        {name: 606, content: "acme corp deposit cheque"}
       ]

案例 2.)

输入:“亚马逊”或“亚马逊”

预期输出:

       [
        {name: 789, content: "Amazon gift card"},
        {name: 646, content: "amazon gift card"}
       ]

案例 3.)

输入:“Dunkin”或“dunkin”或“Dunkin-Donuts”或“dunkin-donuts”或“dunkin-Donuts”或“Dunkin-donuts”或“donuts”或“Donuts”

预期输出:

       [
        {name: 879, content: "Dunkin-Donuts"},
        {name: 656, content: "Dunkin Donuts"}
       ]

谢谢!任何帮助将不胜感激。

【问题讨论】:

标签: javascript mongodb nlp keyword-search


【解决方案1】:

$regex

您可以使用$regexi 标志来区分大小写:

transactions.find( { content: { $regex: /acme/i } } )

$text

或使用$text(以获得更好的性能),首先在包含要搜索的文本的字段上创建text 索引

transactions.createIndex( { content: "text" } )

然后:

transactions.find( { $text: { $search: "acme" } } )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2018-09-27
    • 1970-01-01
    相关资源
    最近更新 更多