Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

mongodb - Searching in mongo db using mongoose regex vs. text

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...