在多列中使用正则表达式进行预搜索 [英] mognoose searching with regular expression in multiple columns

查看:83
本文介绍了在多列中使用正则表达式进行预搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个猫鼬模型,其模式定义为-

I have a mongoose model with schema defined as -

var campusNotesSchema = mongoose.Schema({
    noteId:{type:String, unique:true, default: uuid.v4()},
    title: {type:String, required:'{PATH} is required!'},
    uploader:{type:String,required:'{PATH} is required!'},
    department:{type:String},
    college:{type:String},
    author:{type:String,required:'{PATH} is required!'},
    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);

现在,我想从请求对象参数中搜索诸如

Now I want to search in the title, tags, description field from the request objects parameters something like

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

现在,我如何确保也包括在标题或描述中找到该术语的所有结果,而不仅是两个结果都存在. 另外,如何在标签数组中搜索匹配的字符串

Now how do i make sure that any results in which the term is found in either title or description is also included and not only the ones which are there in both of them. Also, how do i search in the tags array for the matching strings

推荐答案

您可以在猫鼬中使用 $ or 运算符返回匹配项中的任意一个结果
$ or http://docs.mongodb.org /manual/reference/operator/query/or//

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);
})

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

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/

这篇关于在多列中使用正则表达式进行预搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆