sorting regex find mongodb mongoose sorting regex find mongodb mongoose mongoose mongoose

sorting regex find mongodb mongoose


$regex is use only for field type String.It doesn't work with other types

Let Consider small Example for Playing with regex operator

//Let take User Model as below

var mongoose = require('mongoose');var Schema = mongoose.Schema;var UserSchema = new Schema({    Name:String,    Age:Number,    Sex:Boolean,    EmailID:String,    PhoneNumber:String,    Description:String,    created_at:Date,    updated_at:Date});module.exports = mongoose.model('UserSchema', UserSchema);

Your mongodb query for multi search with $regex operator and $options for multi-search will be like below

var toSearch = req.body.searchvalue;var query = {    $or:[        {            "Name":{                $regex:toSearch,                $options:"i"       //it will match both upper case and lower case            }        },        {            "EmailID":{                $regex:toSearch,                $options:"i"       //it will match both upper case and lower case            }        },        {            "PhoneNumber":{                $regex:toSearch,                $options:"i"       //it will match both upper case and lower case            }        },{            "Description":{                $regex:toSearch,                $options:"i"       //it will match both upper case and lower case            }        }    ]}//Important point to remember  we cannot use $regex for age,sex,created_at,updated_at because this field are not StringUserSchema.find(query).toArray(function (err,SearchData) {    if(!err){        //perform your operations    }}) ;

Might this may help you and it is best to create search engine in mongodb. it is next effective to Elastic Search which is no.1