2016-09-25 70 views
0

之間你好請幫我解決這個問題貓鼬查詢日期時間

我需要在貓鼬 2016-09-25T00之間在查詢日期時間數據:00 1:10.000 + 07:00至2016-09- 25T01:00:10.000 + 07:00

上robomongo我用這個查詢它的工作找到

db.getCollection('positions').find({unit_id:"863835025684908", 
utc_ts : { 
    $gte: ISODate("2016-09-25T00:00:10.000+07:00"), 
    $lt: ISODate("2016-09-25T01:00:10.000+07:00") 
    } 
}) 

我造成

{ 
"_id" : ObjectId("57e6bd10fa10a661c4145870"), 
"updated_at" : ISODate("2016-09-25T00:51:12.673+07:00"), 
"created_at" : ISODate("2016-09-25T00:51:12.673+07:00"), 
"unit_id" : "863835025684908", 
"utc_ts" : ISODate("2016-09-25T00:51:10.000+07:00"), 
"recv_utc_ts" : ISODate("2016-09-25T00:51:12.000+07:00"), 
"lat" : 14.127961, 
"lon" : 100.621365 
} 

但是,當我對貓鼬查詢我用這個

var startDate = moment(req.params.startTime).add('hours',7); //req.params.startTime = 2016-09-25 00:00:00 
var endDate = moment(req.params.endTime).add('hours',7); //req.params.endTime = 2016-09-25 01:00:00 

//Find 
Positions.find({ 
    unit_id: req.params.unit_id, 
    utc_ts: { 
     $gt: new Date(startDate).toISOString(), 
     $lt: new Date(endDate).toISOString() 
    } 
}, function(err, positions) { 
    if (err) { 
     return err 
    } 
    else { 
     //console.log(positions); 
     res.json(positions); 
    } 
}); 

我看到貓鼬調試表演

Mongoose: positions.find({ utc_ts: { '$gt': new Date("Sat, 24 Sep 2016 00:51:10 GMT"), '$lt': new Date("Sat, 24 Sep 2016 01:08:36 GMT") }, unit_id: '863835025684908' }, { fields: undefined }) 

然後我從查詢

回答

0

空數組我不認爲你需要使用Date對象。您可能喜歡使用momentjsformat()功能。這裏將代碼來獲取位置

var startDate = moment(req.params.startTime).utcOffset('+0700').format("YYYY-MM-DDTHH:mm:ss.SSSZ"); //req.params.startTime = 2016-09-25 00:00:00 
var endDate = moment(req.params.endTime).utcOffset('+0700').format("YYYY-MM-DDTHH:mm:ss.SSSZ"); //req.params.endTime = 2016-09-25 01:00:00 

//Find 
Positions.find({ 
    unit_id: req.params.unit_id, 
    utc_ts: { 
     $gt: startDate, 
     $lt: endDate 
    } 
}, function(err, positions) { 
    if (err) { 
     return err 
    } 
    else { 
     //console.log(positions); 
     res.json(positions); 
    } 
}); 
+0

是的,我試過,但還是空的數據 – Wetrue

+0

那麼你可以嘗試'UTCOFFSET(「+ 0700」)''的功能momentjs'。我也更新了代碼 –

+0

我用你的編輯代碼仍然不行。 – Wetrue