跟着教程做了一个node.js+mongodb的站点,其中在Schema中对集合的定义如下:
var JobSchema = new mongoose.Schema({
jobtype:String,
title:String,
content:String,
meta:{
createAt:{
type:Date,
default:Date.now()
},
updateAt:{
type:Date,
default:Date.now()
}
}
})在statics中,调用数据库的方法如下:
JobSchema.statics = {
//fetch用于取出数据库中所有数据
//findById用于根据id取出单条数据
fetch: function(cb){
return this
.find({})
.sort('meta.updateAt')
.exec(cb)
},
findById:function(id, cb){
return this
.findOne({_id:id})
.exec(cb)
}
}但如果想根据updateAt或createAt的时间段来查询,不知道应该如何写
以下是我试着写的,但不能查出结果,也没有错误提示信息,只是返回值为空
findByDate:function(dates, cb){
return this
.find({meta:{updateAt:{$gte:dates}}})
.sort('meta.updateAt')
.exec(cb)
}
醉生梦死012
醉生梦死012
随时随地看视频慕课网APP