mongoose新手,内嵌文档时间类型如何查询

跟着教程做了一个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
浏览 3311回答 1
1回答

醉生梦死012

在Mongodb Shell下查询db.jobs.find(),返回结果如下:{ "_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" : "今日作业", "content" : "哇咔咔咔", "meta" : { "updateAt" : ISODate("2017-03-01T06:39:04.517Z"), "createAt" : ISODate("2017-03-01T06:39:04.517Z") }, "__v" : 0 }
打开App,查看更多内容
随时随地看视频慕课网APP