mognoose searching with regular expression in multiple columns

前端 未结 2 1808
情书的邮戳
情书的邮戳 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/

    0 讨论(0)
  • 2020-12-12 03:41

    use this query ,$option = i, will skip the lower /upper case when you are inserting data into TAG array first make it lowercase and search with lowercase this would be easy.

    Campusnotes.find({title:{'$regex':searchText,'$option':'i'},description:{'$regex':searchText,'$option':'i'},tags:searchText}, function(err, collection){
    res.send(collection);
    });
    
    0 讨论(0)
提交回复
热议问题