0

I'm trying to find multiple words in a search string.

For example if I have to find a record that the message contains words: "first" and "second":

 { _id: 1, message: "first and second word" },
 { _id: 2, message: "first only" },
 { _id: 3, message: "second only" },
 { _id: 4, message: "second and first" }

It should returns:

{ _id: 1, message: "first and second word" },
{ _id: 4, message: "second and first" }

I tried:

db.messages.find({ $text: {$search: "second first"} })

But it works like an OR, returns records that contains only one word.

how could I achieve this in Mongo?

1 Answers1

0

try like this:

db.messages.find("text", {search:"\"second\" \"first\""})

mongo docs text search