MongoDB 正则表达式
查询值可直接写 /pattern/ 或 $regex 选项,配合索引时注意前缀锚定(/^abc/)才能走索引。
Key PointsMongoDB,正则,$regex,options,前缀匹配
讲解
查询值可直接写 /pattern/ 或 $regex 选项,配合索引时注意前缀锚定(/^abc/)才能走索引。
示例
JavaScript
db.site.find({ title: /^G/ }) // JS 正则
db.site.find({ title: { $regex: "oo", $options: "i" } }) // 不区分大小写Output
{ title: "Google", ... }讲解
查询值可直接写 /pattern/ 或 $regex 选项,配合索引时注意前缀锚定(/^abc/)才能走索引。
示例
JavaScript
db.site.find({ title: /^G/ }) // JS 正则
db.site.find({ title: { $regex: "oo", $options: "i" } }) // 不区分大小写Output
{ title: "Google", ... }