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

前端之家收集整理的这篇文章主要介绍了正则表达式 – 在多列中使用正则表达式进行mognoose搜索前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个mongoose模型,模式定义为 –
  1. var campusNotesSchema = mongoose.Schema({
  2. 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]
  3. });
@H_502_3@并且模型定义为 –

  1. var Campusnotes = mongoose.model('Campusnotes',campusNotesSchema);
@H_502_3@现在我想从请求对象参数中搜索标题,标签,描述字段

  1. if(req.query.searchText){
  2. Campusnotes.find({title:new RegExp(searchText,'i'),description:new RegExp(searchText,'i')}).exec(function(err,collection) {
  3. res.send(collection);
  4. })
  5. }
@H_502_3@现在我如何确保在标题或描述中找到该术语的任何结果也包括在内,而不仅仅包括两者中的结果.
另外,我如何在tags数组中搜索匹配的字符串

您可以在mongoose中使用$或operator来返回任何匹配项的结果
$或 http://docs.mongodb.org/manual/reference/operator/query/or/
  1. Campusnotes.find({'$or':[{title:new RegExp(searchText,'i')},{description:new RegExp(searchText,'i')}]}).exec(function(err,collection) {
  2. res.send(collection);
  3. })
@H_502_3@要在数组中搜索匹配的字符串,您需要使用mongo的$in运算符:

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

猜你在找的正则表达式相关文章