Can anyone please explain the difference between:
db.collection.find({ $text: { $search: "dog cat" } })
and
Product.find({ "drug": { "$regex": "cols", "$options": "i" } })
When should we go for which one ?
Well, both regex and text search ($text) help you to search in text very efficiently. Both have their own advantages and disadvantages, But there are two clear distinctions
regex
Regex doesn't take advantage of indexes, unless you are searching in beginning of string using ^
operator.
Regex allows you to search partial text. therefore .* and so many other patterns.
Regex doesn't support stop or noise words.
$text
text indexes in mongodb are really fast and should be preferred. However, MongoDB does not implement full featured text indexes. One main drawback is, it doesn't support partial match. e.g. if you are searching for cat, it will search only for cat and cats but not bobcat or caterpiller.
Bottom line is if you are looking to implement feature like RDBMS
like
operator, '$text' will not help you (at least in current implementations of MongoDB, but in future it may change).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With