【发布时间】:2015-07-30 00:24:45
【问题描述】:
我有一个由带有日期字段的元素数组组成的猫鼬模式
var querySchema = new Schema({
id : String,
description: String,
results : [{
date : { type: Date, default: new Date },
result: Object
}]
});
当我添加新元素时,我使用New Date。产生如下元素:
{
"_id" : ObjectId("55b96289c9dcbe79207c4b08"),
"id" : "11",
"description" : "List of graphs loaded in the KB (24 graphs as of 09/05/2015 + 5 default Virtuoso graphs)",
"results" : [
{
"result" : "",
"_id" : ObjectId("55b96289c9dcbe79207c4b09"),
"date" : ISODate("2015-07-29T22:00:00.000Z")
},
{
"result" : "",
"_id" : ObjectId("55b962e05489d4de20a99f87"),
"date" : ISODate("2015-07-31T22:00:00.000Z")
},
{
"result" : "",
"_id" : ObjectId("55b963284f5083492119ef5b"),
"date" : ISODate("2015-08-29T22:00:00.000Z")
}
],
"__v" : 0
}
无论时间如何,我都想查找特定日期范围内的结果。我想要做的是通过查询参数接受他想要的范围的用户输入,如果没有,那么范围将适用于今天的元素。
router.route('/aggregate')
.get(function(req, res) {
if (req.param('id')) {
// Extract the date range filterin query paramteres if they were passed
var start = new Date(req.param('start')) || new Date;
var end = req.param('end') ? new Date(req.param('end')) : new Date;
Query.aggregate([
{
"$match":
{
id : req.param('id'),
"results.date": { "$lt": end, "$gt": start }
},
}],function(err,result) {
res.json(result);
});
} else res.json({message: 'Invalid request. Please specify a query ID'});
});
例如,我尝试使用参数?id=11&start="2014, 7, 31" 进行调用,它显示了 7 月 29 日的结果。各种查询都不起作用,所以我知道有问题。
我尝试了各种日期格式,删除时间是重置它,但到目前为止没有任何效果。
不胜感激。
【问题讨论】:
标签: javascript node.js mongodb date mongoose