mongoose模糊查询一个实例
发布于 11 年前 作者 sdfcbs91 11681 次浏览 最后一次编辑是 8 年前

不会弄图片,要不贴上案例效果就一目了然了。 mongoose模糊查询我现在的理解就是–验证规则(RegExp)。 只要能够匹配,则此行就返回值。 代码: 前端:var searchnotes = function(t,e){ //文章列表模糊查询, if(e.which == 13){ var $t = $(t); var params = {title:$(t).val()} $.ajax({ url:"/searchnotes", type:“post”, data: params, dataType: ‘json’, success: function(res){ if(res.status == 0){ var $div = $t.parent().parent(); $div.children(“li”).remove(); $div.children(".pull-left").remove();

         		for(i in res.notes){
         			$div.append('<div class="pull-left mr5 col_ff99">'+(parseInt(i)+1)+'.</div>');
					var li = '<li id="'+res.notes[i]._id+'" class="none_outside" onclick="postnoteid(this);">';
					li += '<h4 class="cur_point" style="margin-bottom:0;"><span class="pull-left rowlength600 hover1">'+res.notes[i].title +'</span>';
					li +='<span class="pull-right fc07 mr25">'+res.date[i]+'</span><div class="clear"></div></h4>';
					li += '<p class="w600">'+res.notes[i].content.showlength(300).blankString(4)+'</li>'
					$div.append(li);
         		}
         	}else{
         		window.location.href = "/logout";
         	}
      	},
      	error: function (msg) {
        	if ( msg.statusText == 'timeout' )  alert('网络超时');
        	else  $("#ss1").html("更新失败").show();;
        
      	}
	});
}

} 后端: var Note = require("./…/moudels/notes.js"); app.post("/searchnotes",function(req,res){//利用post接收,用已做好的Note类的findRegx方法处理 Note.findRegx(req,res); });

//Note类 Notes.prototype.findRegx=function(req,res){ if(typeof req.session.name !=“undefined”){ Note.find({title:new RegExp(req.body.title),username:req.session.name},function(err,persons){// var arr = new Array(); for(i in persons){ arr[i] = persons[i].update.getFullYear()+"."+fo_mat(persons[i].update.getMonth()+1)+"."+fo_mat(persons[i].update.getDate())+" “ +(persons[i].update.getHours())+”:"+fo_mat(persons[i].update.getMinutes()); } res.send({status:0,notes:persons,date:arr}); }); } else{ res.send({status:1}); }

} module.exports = new Notes();

6 回复

date可以在model中声明获取的格式的

之前用返回出来的值调用format,结果是此类型无该方法, 声明获取的格式,应该怎么写?

见api:

function dob (val) { if (!val) return val; return (val.getMonth() + 1) + “/” + val.getDate() + “/” + val.getFullYear(); }

// defining within the schema var s = new Schema({ born: { type: Date, get: dob })

// or by retreiving its SchemaType var s = new Schema({ born: Date }) s.path(‘born’).get(dob)

返回的就是一个格式化的日期了,如果确实需要的话

嗯,时间格式根据css样式调整的时候,需要格式化一下 在模型的对象设置了时间格式,牛逼,学习了。

@sdfcbs91 根据api修改了,不是模型的对象设置,是在声明一个新模型时定义get. function dob(val) { if (!val) return val; return fo_mat(val.getFullYear()) + “.”+fo_mat((val.getMonth() + 1)) + “.” + fo_mat(val.getDate()) +" " + fo_mat(val.getHours())+":"+fo_mat(val.getMinutes()); } var NoteSchema = new Schema({ title:String ,username:String ,content:String ,innerdate: { type: Date, default: Date.now, get: dob } ,update:{type:Date, default: Date.now,get: dob} });

var Note = db.model(‘notes’,NoteSchema);

@sdfcbs91 or by retreiving its SchemaType 这是两种方法

回到顶部