How to query MongoDB with “like”?

后端 未结 30 2179
予麋鹿
予麋鹿 2020-11-21 05:51

I want to query something with SQL\'s like query:

SELECT * FROM users  WHERE name LIKE \'%m%\'

How to do I achieve the same in

30条回答
  •  Happy的楠姐
    2020-11-21 06:13

    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

提交回复
热议问题