我在MongoDB中比較日期檢索數據
{ "_id" : ObjectId("4f0ee7310b09f7a254000001"), "createdAt" : ISODate("2012-01-12T23:58:28Z") }
{ "_id" : ObjectId("4f0ee7350b09f7a254000002"), "createdAt" : ISODate("2012-01-12T23:58:28Z") }
{ "_id" : ObjectId("4f0ee855e63cecb654000001"), "createdAt" : ISODate("2012-01-13T00:03:59Z") }
{ "_id" : ObjectId("4f0ee859e63cecb654000002"), "createdAt" : ISODate("2012-01-13T00:04:08Z") }
{ "_id" : ObjectId("4f0ee97c212d70bc54000001"), "createdAt" : ISODate("2012-01-13T00:08:54Z") }
{ "_id" : ObjectId("4f0ee99f212d70bc54000002"), "createdAt" : ISODate("2012-01-13T00:09:27Z") }
我只想顯示基於日期的記錄數據是這樣的。檢查我的代碼,它返回我什麼
//dateStr = '120112' or '120113'
var year = '20' + dateStr.substring(0,2);
var month = dateStr.substring(2,4);
var day = dateStr.substring(4);
dateStr = new Date(year,month,day,0,0,0);
var nextDate = new Date(year,month,day,23,59,59);
GPSData.find({"createdAt" : { $gte : dateStr, $lte: nextDate }}, function(err, data) {
if(err)
console.log(err);
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
var body = JSON.stringify(data);
res.end(body); //no output here just []
});
但蒙戈外殼使用此命令我得到的日期「120112」的結果
db.gpsdatas.find({"createdAt" : {new ISODate("2012-01-12T00:00:00Z"), $lte: new ISODate("2012-01-12T23:59:59Z") }});
不知道怎麼貓鼬的作品,但你可能會與{ 「createdAt」:{$ GTE:dateStr}, 「createdAt」:{$ LTE:nextDate}}嘗試 – milan 2012-01-13 13:42:52