MongoDB和全文搜索是单词的一部分 [英] MongoDB and fulltext search part of the word
问题描述
我需要找到单词的一部分,如果我搜索有关len的内容,请找到len开头的所有单词.
I need to find a part of the word, if i search about len its shut find all the words there starting by len.
我正尝试在MongoDB中使用全文本搜索,当它命中100%的单词而不仅仅是单词的一部分时,它会很好地工作.
i'm trying to use fulltext search in MongoDB and its working well when its hit 100% words and not just a part of the words.
为了让这个问题起作用,我对我抱有沉重的解决之道吗?
Have sombardy a soluion to me to get this issue to working?
可以正常工作的示例代码:
db.getCollection('product').find({
'$and' : [{
'$text': {
'$search' : 'lenovo', // match word
'$caseSensitive' : false
},
'$text': {
'$search' : 'y50 b50 y700', // synonym prepared
'$caseSensitive' : false
}
}]
})
我想获得相同结果的示例代码:
db.getCollection('product').find({
'$and' : [{
'$text': {
'$search' : 'len',
'$caseSensitive' : false
},
'$text': {
'$search' : 'y50 b50 y700',
'$caseSensitive' : false
}
}]
})
感谢您的时间,希望我能在MongoDB中进行全文搜索得到帮助
Thanks for your time, and hope i can get help for this fulltext search in MongoDB
推荐答案
如果您想在mongo中进行全文搜索,则应使用$ regex来使用此查询,
If you want full text search in mongo then you should go with $regex by using this query will be
db.product.find({name:{$regex:/len/i}})
有关更多信息,您也可以阅读文档. https://docs.mongodb.com/manual/reference/operator/query/regex/
for more you can read the documentation as well. https://docs.mongodb.com/manual/reference/operator/query/regex/
这篇关于MongoDB和全文搜索是单词的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!