Mongoose.js: Find user by username LIKE value

Mongoose.js: Find user by username LIKE value

Asked on January 21, 2019 in MongoDB.
Add Comment


  • 22 Answer(s)

    Try this code:

    var name = 'Peter';
    model.findOne({name: new RegExp('^'+name+'$', "i")}, function(err, doc) {
    //Do your action here..
    });
    

     

    Answered on January 21, 2019.
    Add Comment

    Try this below code to solve your issue:

    var data = 'Peter';
    db.User.find({'name' : new RegExp(data, 'i')}, function(err, docs){
    cb(docs);
    });
    

     

    Answered on January 21, 2019.
    Add Comment

    You can do following code

    
    db.users.find( { 'username' : { '$regex' : req.body.keyWord, '$options' : 'i' } } )
    
    
    Answered on January 21, 2019.
    Add Comment

    collection.findOne({
    username: /peter/i
    }, function (err, user) {
    assert(/peter/i.test(user.username))
    })

    Answered on January 23, 2019.
    Add Comment

    var Person = mongoose.model(‘Person’, yourSchema);
    Person.findOne({ “name” : { $regex: /Ghost/, $options: ‘i’ } },
    function (err, person) {
    if (err) return handleError(err);
    console.log(‘%s %s is a %s.’, person.name.first, person.name.last, person.occupation);
    });

    Answered on January 26, 2019.
    Add Comment
    router.route('/product/name/:name')
    .get(function(req, res) {
    
        var regex = new RegExp(req.params.name, "i")
        ,   query = { description: regex };
    
        Product.find(query, function(err, products) {
            if (err) {
                res.json(err);
            }
    
            res.json(products);
        });
    
    });  
    Answered on January 26, 2019.
    Add Comment
    var name = 'vijay';
    model.findOne({name: new RegExp('^'+name+'$', "i")}, function(err, doc) {
      //do it here
    })
    Answered on January 26, 2019.
    Add Comment
    if (userId == 'admin')
      userId = {'$regex': '.*.*'};
    User.where('status', 1).where('creator', userId);
    Answered on January 28, 2019.
    Add Comment

    var Person = mongoose.model(‘Person’, yourSchema);
    // find each person with a name contains ‘Ghost’
    Person.findOne({ “name” : { $regex: /Ghost/, $options: ‘i’ } },
    function (err, person) {
    if (err) return handleError(err);
    console.log(‘%s %s is a %s.’, person.name.first, person.name.last, person.occupation)
    ;

    });

    Answered on January 30, 2019.
    Add Comment
    router.route('/product/name/:name')
    .get(function(req, res) {
    
        var regex = new RegExp(req.params.name, "i")
        ,   query = { description: regex };
    
        Product.find(query, function(err, products) {
            if (err) {
                res.json(err);
            }
    
            res.json(products);
        });
    
    });  
    Answered on January 30, 2019.
    Add Comment


  • Your Answer

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