Checking if a field contains a string



  • 3 Answer(s)

    You try this below example code:

    db.users.findOne({"username" : {$regex : ".*son.*"}});
    
    

     

    Answered on December 24, 2018.
    Add Comment

    Regex support from mongo shell, that’s completely possible.

    db.users.findOne({"username" : /.*son.*/});
    

    If we need the query to be case-insensitive, we can use “i” option, like shown below:

    db.users.findOne({"username" : /.*son.*/i});
    

    Look this: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

     

    Answered on December 24, 2018.
    Add Comment

    See: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

    The MySQL

    SELECT * FROM users WHERE username LIKE "%Son%"
    
    

    The MongoDB

    db.users.find({username:/Son/})
    
    
    Answered on December 24, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.