一.思路
a.使用new Date(2017,05,12).getTime()返回截止到今天的毫秒數(shù)
b.然后將上面的毫秒數(shù)減去一天的毫秒數(shù)86?400?000
c.然后就通過這兩個參數(shù)在MongoDB里面查詢今天一天的數(shù)據(jù)
二.科普
a.時間
// 簡單的一句代碼vardate =newDate(時間戳);//獲取一個時間對象/**
1. 下面是獲取時間日期的方法橄仆,需要什么樣的格式自己拼接起來就好了
2. 更多好用的方法可以在這查到 -> http://www.w3school.com.cn/jsref/jsref_obj_date.asp
*/date.getFullYear();// 獲取完整的年份(4位,1970)date.getMonth();// 獲取月份(0-11,0代表1月,用的時候記得加上1)date.getDate();// 獲取日(1-31)date.getTime();// 獲取時間(從1970.1.1開始的毫秒數(shù))date.getHours();// 獲取小時數(shù)(0-23)date.getMinutes();// 獲取分鐘數(shù)(0-59)date.getSeconds();// 獲取秒數(shù)(0-59)
b.數(shù)據(jù)庫操作
$lt,$lte,$gt,$gte.分別對應(yīng): <,<=,>,>=. 該字段是用在condition中的.如果,你想要鏈?zhǔn)秸{(diào)用,則需要使用
lt,lte,ge,gte.
eg:
model.find({num:{$gt:12}},cb)
model.where(‘num’).gt(12).exec(cb)
User.find({age: {$gte:21, $lte:65}}, callback);//等價于:User.where('age').gte(21).lte(65).exec(callback);
三.閑言少敘,書歸正傳,
直接上代碼,我就是這么簡單粗暴!
varmid=mem[0].mid; ?//mid是會員的id
tplData.mid=mid;
//獲取今天的健康記錄,有就返回沒有就返回空的數(shù)組
vardate=newDate();Y=date.getFullYear() +'-';
M= (date.getMonth()+1<10?'0'+(date.getMonth()+1) :date.getMonth()+1) +'-';
D=date.getDate() +' ';
varend=newDate(Y+M+D).getTime()//今天結(jié)束時的毫秒數(shù)
varstart=end-86400000//今天開始時的毫秒數(shù)
console.log("今天的毫秒數(shù)"+end)
//下面這個表示查詢mid是mid的會員的開始時間到結(jié)束時間的數(shù)據(jù),也就是該會員今天一天上傳的數(shù)據(jù)
Healthrecord.find({mid:mid,timestamp:{$gte:start,$lte:end}},function(err,health){
console.log("從后臺查詢出來的數(shù)據(jù)"+health)
tplData.healthrecord=health;//賦值然后傳輸?shù)角岸?/p>
});