[英]get today's imported data from mongodb using timestamp node js
我按當前時間戳將報告添加到 MongoDB 數據庫中,但我無法檢索今天導入的數據,因為當前時間戳與插入數據的時間戳值不同。 如何使用時間戳查看數據?
添加報告
reportsController.addReports = function (req, resp) {
let battriesObjs = [];
let banksObjs = [];
let reports = req.body;
// console.log(batteries);
const siteReports = new SiteReports({
siteId: reports.siteId,
environmentParam1: reports.envp1,
environmentParam2: reports.envp2,
environmentParam3: reports.envp3,
reportDate: moment().valueOf()
});
let btr = reports['battries'];
btr.forEach(function (btry) {
const batteryReports = new BatteryReports({
batteryId: btry.bid,
batteryStatus: btry.status,
batteryVoltage: btry.voltage,
batteryTemperature: btry.temperature,
reportDate: moment().valueOf()
});
battriesObjs.push(batteryReports);
});
siteReports
.save()
.then(value => {
console.log("insert sampleCollection result ", value);
BatteryReports
.insertMany(battriesObjs)
.then(valueBatteries =>
resp.status(status.OK)
.json({'respond': 'the document is updated successfully'}))
.catch(err => {
console.log("bulk insert sampleCollection error ", err);
});
})
.catch(err => {
console.log("bulk insert sampleCollection error ", err);
});
};
得到回報
reportsController.getAllTodayBatteryReports = function (req, resp) {
console.log(req.query.batteryBankId, moment().valueOf());
BatteryReports.aggregate([
{
$match: {
"reportDate": moment().valueOf().toString()
}
}
,
{
$lookup:
{
from: "batteryschemas",
localField: "batteryId",
foreignField: "batteryId",
as: "batteryReports_with_their_info"
}
}
,
{
$match:
{"batteryReports_with_their_info.batteryBankId": new mongoose.Types.ObjectId(req.query.batteryBankId)}
}
,
{
$replaceRoot: {newRoot: {$mergeObjects: [{$arrayElemAt: ["$batteryReports_with_their_info", 0]}, "$$ROOT"]}}
}
,
{
$project: {
batteryReports_with_their_info: 0,
batteryBrand: 0,
batteryMaximumChargingCurrent: 0,
batteryCycleOfCharge: 0,
batteryModel: 0,
batteryProductDate: 0,
batteryInternalResistance: 0,
batteryCapacity: 0,
__v: 0
}
}
], function (err, result) {
console.log(result);
if (err) {
resp.status(status.NOT_FOUND).json({'respond': err + ''});
} else {
resp.status(status.OK).json(result);
}
// console.log(result);
});
};
如您所見,我的這部分代碼獲取當前時間戳並將其放入 MongoDB 匹配方法,但盡管在今天的時間戳中添加了一些數據,但我無法通過當前時間戳值獲取這些記錄。
$match: {
"reportDate": moment().valueOf().toString()
}
樣品
{
"_id": "5d3c9116ee51fe32b44160f6",
"batteryId": "1",
"batteryVoltage": "5.5",
"batteryVoltageMin": "34",
"batteryVoltageMax": "34",
"batteryMinTemperature": "443",
"batteryMaxTemperature": "43",
"batteryBankId": "5d29c1469e0e8b3afcf3a1e6",
"batteryStatus": "H",
"batteryTemperature": "38",
"reportDate": "1564250390757"
}
您可以將moment().valueOf()
值分配給對象並將其用於 reportDate。 加上moment().valueOf()
將以毫秒為單位為您提供時間戳,然后插入的時間將與查詢時間不同,因此您需要進行范圍查詢以獲得特定的記錄/文檔,或者傳遞精確的時間戳以獲得精確匹配。
假設您今天插入了幾個文檔,並希望在同一天在給定的時間戳運行您的查詢(請注意時間戳屬於同一區域)然后,
let currentTimeStamp = moment().valueOf().toString()
let startOfDayTimeStamp = moment().startOf('day').valueOf().toString()
然后,
$match: {
"reportDate": { $gt: startOfDayTimeStamp, $lt: currentTimeStamp }
}
如果您正在處理頻繁寫入的集合,那么您正在處理像這樣的讀取查詢的大量數據,請記住進行適當的索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.