我有一个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/
原文链接:https://www.f2er.com/regex/357276.html$或 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/