How to query MongoDB with "like"?
I want to query something as SQL's like
query:
select *
from users
where name like '%m%'
How to do the same in MongoDB?
I can't find a operator for like
in the documentations.
That would have to be:
db.users.find({"name": /.*m.*/})
or, similar:
db.users.find({"name": /m/})
You're looking for something that contains "m" somewhere (SQL's ' %
' operator is equivalent to Regexp's ' .*
'), not something that has "m" anchored to the beginning of the string.
db.users.insert({name: 'paulo'})
db.users.insert({name: 'patric'})
db.users.insert({name: 'pedro'})
db.users.find({name: /a/}) //like '%a%'
out: paulo, patric
db.users.find({name: /^pa/}) //like 'pa%'
out: paulo, patric
db.users.find({name: /ro$/}) //like '%ro'
out: pedro
In
you can do:
db.users.find({'name': {'$regex': 'sometext'}})
链接地址: http://www.djcxy.com/p/15154.html
上一篇: MongoDB / NoSQL:保持文档更改历史记录
下一篇: 如何用“like”查询MongoDB?