问题
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 ?
回答1:
$regex
Provides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.36 with UTF-8 support.
$text
performs a text search on the content of the fields indexed with a text index.
For data
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" }
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
To search field sku
which is single string value, could use $regex
db.products.find( { sku: { $regex: /^ABC/i } } )
Whereas to search field description
which is text content value, could use $text
, of course, we should build text index on description
firstly.
db.products.find( { $text: { $search: "coffee" } } )
Why not only regex?
regular expressions
have their natural limitations because they lack any stemming functionality and cannot handle convenient search queries such as“action -superhero”
in a trivial way.- they cannot use traditional indexes which makes queries in large datasets really slow.
Here is one link to compare them.
回答2:
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).
来源:https://stackoverflow.com/questions/35812680/searching-in-mongo-db-using-mongoose-regex-vs-text