Like AND AND NOT in MongoDB $ text search

I apologize in advance if this question is asked, I searched, I can not find the answer.

I want to do a search in MongoDB, and create an index and do something like

db.myCollection.runCommand( "text", { search: "myWord" } )

This works great.

I can also do

db.myCollection.runCommand( "text", { search: "myWord1 myWord2" } )

and he will search for both words.

Can I perform the above search by asking to find BOTH words? (I know that I can search by the first word, and then search by the second by result, but I wonder if there is a better way).

In addition, is it possible to indicate the words to be rejected (for example, to search for the word "test", but not for "testing").

I would like to know if I can do this in mongoDB without using external tools.

+4
source share
1

, . :

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

"" - , , "" :

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

, -:

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

, , "" :

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

, , :

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

.

. $text. , .

+8

Source: https://habr.com/ru/post/1542857/


All Articles