我正在尝试从另一个后端服务检索 json 数据并将其保存到我自己的本地数据库中 - 以更快地从我的前端加载数据。 目标是用这些数据表示一个图表。
程序如下:
- 借助 axios 获取数据。 (另一个API后端服务)
- 使用 Mongoose 及其函数将其存储到数据库。此处插入Many。
...
.then(resp => {
Object.insertMany(resp, function(err, docs) {
if (err) {
console.log(err)
res.send('Something went wrong. Storing data to db failed.')
}
console.log(
'Successfully stored'
)
res.status(200).end()
})
}
- 将其存储到 MongoDB。
Mongoose 架构:
var ObjectSchema = new Schema({
consumedQuantity: Number,
cost: Number,
body: String,
date: Date,
date_download: { type: Date, default: Date },
product: String,
unitOfMeasure: String
})
<小时/>
问题:
我从 api 后端服务获取以下格式的日期:
{.. "date":"2019-08-08T00:00:00" .. }
但是存储在 MongoDB 中是这样的:
{... "date" : ISODate("2019-08-07T22:00:00Z") ... }
如何解决该问题以及在哪里解决? 我已经阅读了一些答案,但无法翻译并将其用于我的案例。 我可以更改/修改架构吗?
提前谢谢您。
最佳答案
我认为,如果您更改 js Date 响应中的日期应该可以工作。 像这样的事情:
...
.then(resp => {
resp.date_download = new Date(resp.date_download); -> If it is an array just loop through
Object.insertMany(resp, function(err, docs) {
if (err) {
console.log(err)
res.send('Something went wrong. Storing data to db failed.')
}
console.log(
'Successfully stored'
)
res.status(200).end()
})
}
关于javascript - 从另一个后端服务检索的数据(日期和时间)未正确存储在 MongoDB 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57521852/