如何查询日期大于提供日期的文档?
How can I query for documents where date is greater than a provided date?
我有一个事件集合,其中包含以这种格式存储的事件日期: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);
}
})
};
问题是此查询是 return 事件,发生在所提供的 'date' 之前。我做错了什么?
start_date是String,如果要进行日期比较,就把它设为Date。
将架构更改为类似这样的内容;
var eventSchema = mongoose.Schema({
title : String,
details : String,
start_date : Date,
company: {
type: String,
ref: 'Company'
}
});
我有一个事件集合,其中包含以这种格式存储的事件日期: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);
}
})
};
问题是此查询是 return 事件,发生在所提供的 'date' 之前。我做错了什么?
start_date是String,如果要进行日期比较,就把它设为Date。
将架构更改为类似这样的内容;
var eventSchema = mongoose.Schema({
title : String,
details : String,
start_date : Date,
company: {
type: String,
ref: 'Company'
}
});