MongoDB模糊查询$regex
MongoDB使用$regex操作符来设置匹配字符串的正则表达式,使用PCRE(Pert Compatible Regular Expression)作为正则表达式语言
regex操作符:
- {
- {
- {
正则表达式:
- {
regex与正则表达式对象的区别:
- 在$in操作符中只能使用正则表达式对象,例如:{name:{$in:[/^joe/i,/^jack/}}
- 在使用隐式的$and操作符中,只能使用$regex,例如:{name:{$regex:/^jo/i, $nin:['john']}}
- 当option选项中包含X或S选项时,只能使用$regex,例如:{name:{$regex:/m.*line/,$options:"si"}}
-
sql | MongoDB |
---|---|
select * from student where name like ’%test%’ | db.student.find({name:{$regex:/test/}}) |
select * from student where name regexp ’test’ | db.student.find({name:/test/}) |