如何查询日期大于提供日期的文档?

时间:2015-02-26 18:17:42

标签: mongodb mongoose

我有一系列事件,其中包含以此格式存储的事件日期:YYYY-MM-DD。我想查询大于提供日期的所有事件。这就是我到目前为止所做的:

var eventSchema = mongoose.Schema({
    title : String,
    details : String,
    start_date : String,
    company: {
        type: String,
        ref: 'Company'
    }
});

eventSchema.methods.getUpcomingEvents = function(company_id, cb) {
var date = utils.dateToday(); // returns e.g., '2015-02-26'

return this.model('Event')
    .find({ company: company_id, start_date : {$gte: date} })
    .sort({start_date: 'asc'})
    .exec(function (err, data) {
        if (err) {
            console.log('ERROR = ' + err);
            cb(false, err);
        } else {
            cb(null, data);
        }
    })
};

问题是此查询是在提供的“日期”之前发生的返回事件。我做错了什么?

1 个答案:

答案 0 :(得分:0)

start_date是String,如果要执行日期比较,请将其设为Date。

将架构更改为类似的内容;

var eventSchema = mongoose.Schema({
    title : String,
    details : String,
    start_date : Date,
    company: {
        type: String,
        ref: 'Company'
    }
});
相关问题