I am currently working on a project where I need to fetch JSON data from an external backend service and save it locally in my own database. The goal is to improve the loading speed of the data on the frontend, particularly for representing a graph.
Here is the process I am following:
- Retrieve the data using axios from another API backend service.
- Utilize Mongoose and its functions to store the data in the database, specifically using InsertMany.
...
.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()
})
}
- Save the data to MongoDB.
Mongoose Schema:
var ObjectSchema = new Schema({
consumedQuantity: Number,
cost: Number,
body: String,
date: Date,
date_download: { type: Date, default: Date },
product: String,
unitOfMeasure: String
})
Issue:
When fetching dates from the API backend service, they are in this format:
{.. "date":"2019-08-08T00:00:00" .. }
However, when stored in MongoDB, they look like this:
{... "date" : ISODate("2019-08-07T22:00:00Z") ... }
I am seeking advice on how to rectify this issue. I have come across some solutions but have had difficulty applying them to my specific scenario. Is it possible to adjust the Schema to resolve this?
Thank you for your assistance.