加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 在多列中使用正则表达式进行mognoose搜索

发布时间:2020-12-14 06:31:35 所属栏目:百科 来源:网络整理
导读:我有一个mongoose模型,模式定义为 – var campusNotesSchema = mongoose.Schema({ noteId:{type:String,unique:true,default: uuid.v4()},title: {type:String,required:'{PATH} is required!'},uploader:{type:String,department:{type:String},college:{ty
我有一个mongoose模型,模式定义为 –
var campusNotesSchema = mongoose.Schema({
    noteId:{type:String,unique:true,default: uuid.v4()},title: {type:String,required:'{PATH} is required!'},uploader:{type:String,department:{type:String},college:{type:String},author:{type:String,actualFileName: [String],storedFileName: [String],subject: {type:String},description: {type:String},details: {type:String},date: {type:Date,default: Date},tags: [String]
});

并且模型定义为 –

var Campusnotes = mongoose.model('Campusnotes',campusNotesSchema);

现在我想从请求对象参数中搜索标题,标签,描述字段

if(req.query.searchText){        
    Campusnotes.find({title:new RegExp(searchText,'i'),description:new RegExp(searchText,'i')}).exec(function(err,collection) {
        res.send(collection);
    })
}

现在我如何确保在标题或描述中找到该术语的任何结果也包括在内,而不仅仅包括两者中的结果.
另外,我如何在tags数组中搜索匹配的字符串

您可以在mongoose中使用$或operator来返回任何匹配项的结果
$或 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);
})

要在数组中搜索匹配的字符串,您需要使用mongo的$in运算符:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读