mognoose searching with regular expression in multiple columns

前端 未结 2 1816
情书的邮戳
情书的邮戳 2020-12-12 03:13

I have a mongoose model with schema defined as -

var campusNotesSchema = mongoose.Schema({
    noteId:{type:String, unique:true, default: uuid.v4()},
    ti         


        
2条回答
  •  难免孤独
    2020-12-12 03:26

    You can user $or operator in mongoose to return results with either of matches
    $or http://docs.mongodb.org/manual/reference/operator/query/or/

    Campusnotes.find({'$or':[{title:new RegExp(searchText,'i')},{description:new RegExp(searchText,'i')}]}).exec(function(err, collection) {
        res.send(collection);
    })
    

    To search in array for matching strings, you need to use $in operator of mongo:

    $in : http://docs.mongodb.org/manual/reference/operator/query/in/

提交回复
热议问题