mongoDB: PyMongo中包含单个字母的字符串的全文查询

mongoDB: Full text query for strings containing single letters in PyMongo

我在 MongoDB 中有一个集合,其中每个文档包含 titlecolor。我想使用 title 键执行全文搜索。为此,我使用了以下代码:

mycol.create_index([('title', 'text')])
mycol.find({"colors" : color, "$text": {"$search": search_text}}).limit(10)

现在,我想使用以下规范进行查询:

{
    'color' : 'blue',
    '$text' : {
         "$search" : "V Neck"
     }
}

此查询 returns 结果中 "Neck" 作为搜索文本匹配,但 "V" 未出现在结果中。

执行上述查询的结果:

1. Maniac Men's Fullsleeve Round Neck Round Neck All Over Printed Navy Cotton Tshirt
2. Off White Printed Round Neck Tshirt
3. Blue Striped Round Neck Tshirt
4. Grey Printed Round Neck Tshirt
5. Blue Solid Round Neck Tshirt
6. Blue Solid Round Neck Tshirt
7. Red Printed Round Neck Tshirt
8. Blue Printed Round Neck Tshirt
9. Blue Checked Round Neck Tshirt
10. Grey Printed Round Neck Tshirt

有什么方法可以得到匹配整个关键字的结果 "V Neck"?

您需要将 $search 包裹在 double-quotes 中:

"$search" : "\"V Neck\""

搜索精确短语已记录 here:

You can also search for exact phrases by wrapping them in double-quotes. If the $search string includes a phrase and individual terms, text search will only match documents that include the phrase.

For example, the following will find all documents containing “coffee shop”:

db.stores.find( { $text: { $search: "\"coffee shop\"" } } )