【问题标题】:MongoDB text search finds the long string, but not the shorten string? [duplicate]MongoDB 文本搜索找到长字符串,但没有找到短字符串? [复制]
【发布时间】:2020-05-11 03:52:37
【问题描述】:

在 MongoDB 3.6 中,我有一个带有文本搜索索引的数据集合。它可以找到一个单词的较长版本,但不能找到较短的版本,我怎样才能让它找到两个版本?

db.test.createIndex({name: 'text', description: 'text'});
db.test.insert({name: 'MYREALLYLONGNAME', description: 'MYREALLYLONGNAME'});

db.test.find({$text: {$search: 'MYREALLYLONGNA'}});
> FINDS IT

db.test.find({$text: {$search: 'MYREALLYL'}});
> DOES NOT FIND IT

【问题讨论】:

  • 确实如此,“部分文本搜索”是我应该搜索的内容

标签: mongodb


【解决方案1】:

那应该是:

db.test.find({{$text: {$search: /.*MYREALLYL.*/}})
db.test.find( { name: { $regex: /MYREALLYL/ } } )

注意:mongodb 使用的正则表达式比 sql中的“喜欢”。使用正则表达式,您可以创建任何模式 你想象的那样。

有关正则表达式的更多信息,请参阅此链接 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

https://docs.mongodb.com/v3.6/reference/operator/query/regex/

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2013-05-08
    • 2013-06-09
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多