When storing data in MongoDB I have a field start_time
which stores date time as String (ISO String). The data is acquired as follows
- React app sends data to my Express REST API
- Express API forwards data to Third-Party API
- Response from Third-Party API is stored in Mongo DB
In React i send data as start_time: dateTime.toISOString()
.
I have logged Request and Response from both client and API:
for time 2019-09-25 04:00PM GMT 5:30 (IST)
- On Request:
start_time: '2021-09-25T10:30:00.000Z'
- API Response:
"2021-09-25T10:30:00.000Z"
- Stored in DB:
start_time:"2021-09-25T09:41:58Z"
when converted in client side new Date(start_time)
returns the current time at which i created the request.
for time 2019-09-26 05:30PM GMT 5:30 (IST)
- On Request:
start_time: '2021-09-25T12:00:00.000Z'
- API Response:
"2021-09-25T12:00:00.000Z"
- Stored in DB:
start_time:"2021-09-26T06:30:12Z"
when converted to date produces Time 12:00PM
.
I Have a schema defined with Mongoose for field start_time: {type: Date}
I save data to Mongo Db like so: after receiving API Response i store response data to Mongo DB:
axios(config)
.then(function (response) {
response.data["participants"] = participants;
const newMeeting = new Meeting(response.data);
try {
newMeeting
.save()
.exec()
.then((meeting) => {
console.log("Successfully store meeting to database");
});
} catch (err) {
console.error(err);
}
res.json(response.data);
})
.catch(function (err) {
console.error(err);
});
Both client request and API response works perfectly as expected however Mongo Db stores the same date Time differently. how could i tackle this issue?
CodePudding user response:
Use this code when your are saving files
`${file.fieldname}-${Date.now()}`
CodePudding user response:
Send start_time: dateTime
rather than start_time: dateTime.toISOString()
Date/times are always stored as UTC times. Usually the client application takes care to display the time in local time zone.
If you need to preserve the input time zone, then you have to store it separately in an extra field.