我可以通过MongoDB中的$text
和$search
方法搜索完整的单词:
db.users.find({$text: {$search: "John"});
将产生以下结果:
// case sensitive and insensitive - both
{"first_name": "John", "last_name": "Ted", "description": ""}
{"first_name": "Jess", "last_name": "John","description": ""},
{"first_name": "Dan", "last_name": "Brad", "description": "Send the message to John"}
但是,我仍然想找到这些结果,搜索Joh
(注意没有完全匹配项)。
我该怎么做?
我尝试过:
db.users.find({$text: {$search: {$regex: ".*Joh.*"}}});
我也尝试过:
db.users.find({$text: {$search: ".*Joh.*"}});
但是那没用。
注意-请根据$ text和$ search索引提供解决方案