且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MongoDB $ text搜索中进行“与非"操作

更新时间:2023-02-14 22:16:54

您可以在文本搜索中使用一些选项来满足这些需求.请考虑以下文件:

There are a few options that you can use with text search to suit those needs. Consider the following documents:

{ "text" : "cake" }
{ "text" : "sale" }
{ "text" : "sale cake" }
{ "text" : "cake sale" }
{ "text" : "dress sale" }
{ "text" : "cake sale monday" }

默认的单词列表"是包含,但是如果您不想使用包含,则可以引用"这些单词:

The default "list" of words is an or inclusion, but if you wan't an and inclusion you "quote" the words:

db.words.find( { "$text": { "$search": "\"cake\" \"sale\"" } },{_id: 0})
{ "text" : "cake sale" }
{ "text" : "sale cake" }
{ "text" : "cake sale monday" }

如果您要排除一个单词,请以-作为前缀:

If you want to exclude a word then you prefix with -:

db.words.find( { "$text": { "$search": "\"cake\" \"sale\" -monday" } },{_id: 0})
{ "text" : "cake sale" }
{ "text" : "sale cake" }

如果您希望将其中一部分作为精确短语,则可以引用"整个短语:

And if you wanted part of that as an exact phrase then you "quote" the whole phrase:

db.words.find( { "$text": { "$search": "\"cake sale\" -monday" } },{_id: 0})
{ "text" : "cake sale" }

使用词干确实有问题,所以:

Stemming words though does have an issue, so :

db.words.find( { "$text": { "$search": "test -testing" } },{_id: 0})

实际上不会返回结果.

请参阅 $text 上的文档运算符.目前还没有全部,但是越来越好.

See the documentation on the $text operator for examples. Not all are there at the moment, but it's getting better.