I want to make a word boundary search. For example, suppose you have the following entries:
- "the cooks."
- "cooks"
- " cook."
- "the cook is"
- "cook."
And make a search to find entries which contain "cook" as a whole. That is, only the 3th, 4th and 5th entries should be returned.
In this case, when I use \b
word boundary statement, it somehow becomes distorted due to automatic escaping.
import re, pymongo
# prepare pymongo
collection.find({"entry": re.compile('\bcook\b').pattern})
When I print the query dictionary, the \b
becomes \\b
.
My question is how can I make a word boundary search using PyMongo? I am able to do this in MongoDB shell but failed at PyMongo.