MongoDB Regular Expression Search - Starts with using javascript driver and NodeJS

后端 未结 2 1246
-上瘾入骨i
-上瘾入骨i 2020-12-03 14:53

I\'m using the JavaScript mongodb driver from nodejs. I want to do this query in my JavaScript function:

db.mycollection.find({Zip:/^94404/}); 
2条回答
  •  不知归路
    2020-12-03 15:29

    $options => i for case insensitive search

    Start with string

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

    End with string

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

    Contains string

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

    Doesn't Contains string

    db.collection.find({zip:{'$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/

提交回复
热议问题