Case insensitive search in Mongo

前端 未结 5 2079
时光说笑
时光说笑 2020-11-29 21:04

I am using a case insensitive search in Mongo, something similar to https://stackoverflow.com/q/5500823/1028488.

ie I am using a regex with options i. But I am am ha

5条回答
  •  时光说笑
    2020-11-29 21:32

    You can Use $options => i for case insensitive search. Giving some possible examples required for string match.

    Exact case insensitive string

    db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
    

    Contains string

    db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
    

    Start with string

    db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
    

    End with string

    db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
    

    Doesn't Contains string

    db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
    

    Keep this as a bookmark, and a reference for any other alterations you may need. http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

提交回复
热议问题