跟著教程做了一個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中,調(diào)用數(shù)據(jù)庫的方法如下:JobSchema.statics?=?{
????//fetch用于取出數(shù)據(jù)庫中所有數(shù)據(jù)
????//findById用于根據(jù)id取出單條數(shù)據(jù)
????fetch:?function(cb){
????????return?this
????????.find({})
????????.sort('meta.updateAt')
????????.exec(cb)
????},
????findById:function(id,?cb){
????????return?this
????????.findOne({_id:id})
????????.exec(cb)
????}
}但如果想根據(jù)updateAt或createAt的時間段來查詢,不知道應(yīng)該如何寫以下是我試著寫的,但不能查出結(jié)果,也沒有錯誤提示信息,只是返回值為空findByDate:function(dates,?cb){
????return?this
????.find({meta:{updateAt:{$gte:dates}}})
????.sort('meta.updateAt')
????.exec(cb)
}
1 回答

醉生夢死012
TA貢獻(xiàn)1條經(jīng)驗 獲得超0個贊
在Mongodb Shell下查詢db.jobs.find(),返回結(jié)果如下:
{?"_id"?:?ObjectId("58b504e7a5c3412824649bb7"),?"jobtype"?:?"語文",?"title"?:?"哈哈",?"content"?:?"手機可以訪問了",?"meta"?:?{?"updateAt"?:?ISODate("2017-02-28T05:04:39.693Z"),?"createAt"?:?ISODate("2017-02-28T05:04:39.693Z")?},?"__v"?:?0} {?"_id"?:?ObjectId("58b66c88e6673524048a6772"),?"jobtype"?:?"語文",?"title"?:?"今日作業(yè)",?"content"?:?"哇咔咔咔",?"meta"?:?{?"updateAt"?:?ISODate("2017-03-01T06:39:04.517Z"),?"createAt"?:?ISODate("2017-03-01T06:39:04.517Z")?},?"__v"?:?0?}
- 1 回答
- 0 關(guān)注
- 3379 瀏覽
添加回答
舉報
0/150
提交
取消