且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式匹配mongodb中的整个单词

更新时间:2022-11-12 08:00:37

您可以使用\ b(单词边界)运算符,例如:

You can use the \b (word boundary) operator, eg:

{"$or": [{"data.title": {"$regex": /\bHello\b/i}}]}

通过这种方式,搜索"/\ bH \ b/"将与"Hello"不匹配.

In this way, searching for "/\bH\b/" won't match "Hello".

否则,请在标题字段上使用$ text索引,然后进行$ text搜索:

Otherwise, use a $text index on your title field, then do a $text search:

{"$or": [{$text:{$search:"Hello"}}]}

有关文本搜索的更多信息,请参阅以下文档: http://docs .mongodb.org/manual/administration/indexes-text/

For more information about text search, look at the documentation: http://docs.mongodb.org/manual/administration/indexes-text/