please wait

  • Bhanu Gupta Mar-08-2019 05:59:06 AM ( 2 months ago )

    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.

  • Deepak Parmar Mar-08-2019 06:00:34 AM ( 2 months ago )

    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.

  • Sarah Jones Mar-08-2019 06:03:45 AM ( 2 months ago )
    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

  • Yasmin Mirza Mar-08-2019 06:05:16 AM ( 2 months ago )



    In PHP, you could use following code:

    $collection->find(array('name'=> array('$regex' => 'm'));

Please login

Similar Discussion

Recommended For You