Home > Back-end >  How to search text in specific documents and not all documents in mongodb?
How to search text in specific documents and not all documents in mongodb?

Time:11-28

First I created index for my data:

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

and here is the code for searching text inside the indexed field.

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

but the problem is it searches the whole documents in my collection for the word "shop", and I want to limit its explore to some specific documents based on for example their id. In other words, I want to just search the word "shop" through documents in my collection that the value of their Occupation field is equal to "Z1";

CodePudding user response:

You can just add the other field like this:

db.stores.find( { occupation: "Z1", $text: { $search: "shop" } } )

  • Related