mongo在单词数组中搜索字符串匹配的一部分

时间:2022-12-06 21:47:54

I want to find partial string matching in mongodb list element for example my search string is:

我想在mongodb列表元素中找到部分字符串匹配,例如我的搜索字符串是:

"Hello world we are on mars"

“你好世界我们在火星上”

my records tags are:

我的记录标签是:

  1. words : ["hell", "bubu world"]
  2. 字:[“地狱”,“布布世界”]

  3. words : ["we are", "cookie"]
  4. 字:[“我们是”,“饼干”]

  5. words : ["are nono mars", "w"]
  6. 字:[“是nono火星”,“w”]

I want to get bask only record number 2 where one of the array elements is matched

我想获得只有一个数组元素匹配的记录号2的bask

1 个解决方案

#1


2  

This may not be the exact answer you are looking for. However, I have outlined my thoughts in order for you to rethink about the requirement and possible solution.

这可能不是您正在寻找的确切答案。但是,我已经概述了我的想法,以便您重新考虑要求和可能的解决方案。

You may need to rethink about how you wanted to design the solution. You may not be able to achieve what you expected in the single Mongo query because normally the database attributes would have more text and search string would have less words. As per your question, your requirement is opposite to it.

您可能需要重新考虑如何设计解决方案。您可能无法在单个Mongo查询中实现预期,因为通常数据库属性将包含更多文本,搜索字符串将具有更少的单词。根据您的问题,您的要求与之相反。

One possible solution for a typical text search in MongoDB is "Text" Index and use "$text" and "$search" in find.

MongoDB中典型文本搜索的一种可能解决方案是“文本”索引,并在find中使用“$ text”和“$ search”。

https://docs.mongodb.com/manual/reference/operator/query/text/#op._S_text

Create Text Index:-

创建文本索引: -

db.collectionname.createIndex({words : "text"})

db.collectionname.createIndex({words:“text”})

db.words.find( { $text: { $search: "Hello world we are on mars", $caseSensitive: true  } } )

The result would be : 1 and 3

结果将是:1和3

You can also perform phrase search by enclosing the pharse in escaped double quotes (\").

您还可以通过将转义的双引号(\“)括起来进行短语搜索。

#1


2  

This may not be the exact answer you are looking for. However, I have outlined my thoughts in order for you to rethink about the requirement and possible solution.

这可能不是您正在寻找的确切答案。但是,我已经概述了我的想法,以便您重新考虑要求和可能的解决方案。

You may need to rethink about how you wanted to design the solution. You may not be able to achieve what you expected in the single Mongo query because normally the database attributes would have more text and search string would have less words. As per your question, your requirement is opposite to it.

您可能需要重新考虑如何设计解决方案。您可能无法在单个Mongo查询中实现预期,因为通常数据库属性将包含更多文本,搜索字符串将具有更少的单词。根据您的问题,您的要求与之相反。

One possible solution for a typical text search in MongoDB is "Text" Index and use "$text" and "$search" in find.

MongoDB中典型文本搜索的一种可能解决方案是“文本”索引,并在find中使用“$ text”和“$ search”。

https://docs.mongodb.com/manual/reference/operator/query/text/#op._S_text

Create Text Index:-

创建文本索引: -

db.collectionname.createIndex({words : "text"})

db.collectionname.createIndex({words:“text”})

db.words.find( { $text: { $search: "Hello world we are on mars", $caseSensitive: true  } } )

The result would be : 1 and 3

结果将是:1和3

You can also perform phrase search by enclosing the pharse in escaped double quotes (\").

您还可以通过将转义的双引号(\“)括起来进行短语搜索。