我似乎无法在谷歌和猫鼬网站上找到答案,所以我在这里问.如果可能,我如何在文档中搜索部分匹配的字符串.
例如:
在用户集合中:
{ name: "Louis", location: "Paris, France" },
{ name: "Bill", location: "Paris, Illinoid" },
{ name: "Stephen", location: "Toronto, Ontario" }
猫鼬功能:
searchForCity("Paris");
结果将是位于字符串String中的"Paris"的User集合中的文档列表.例如:
[
    { name: "Louis", location: "Paris, France" },
    { name: "Homer", location: "Paris, Illinois" }
]
mu *_*ort 16
你可以使用正则表达式:
Query#regex,Query#$regex指定
$regex运算符.Run Code Online (Sandbox Code Playgroud)query.regex('name.first', /^a/i)
所以像这样:
User.where('location').$regex(/Paris/);
User.where('location').$regex(/paris/i); // Case insensitive version
请记住,正则表达式查询可能非常昂贵,因为MongoDB必须针对每一个运行正则表达式location.如果你可以在开始时锚定你的正则表达式:
User.where('location').$regex(/^Paris/);
然后你可以索引位置,MongoDB将使用该索引.
| 归档时间: | 
 | 
| 查看次数: | 9694 次 | 
| 最近记录: |