I'm using Node.js and Angular.js for a web project. I understand that date is saved as date if it's created on server using new Date() (e.g. 2015-04-08 04:15:18.712Z
shown in Robomongo as Date type). However, if the date is created on client using new Date(), it is then saved as a string (e.g. 2015-04-07T04:58:12.771Z
shown in Robomongo as String type), because it becomes a string through node API. How to make it save as a Date instead of String?
UPDATE: This is what I got based on Jason Cust's input. In node's server.js specify the reviver option as follows:
app.use(bodyParser.json({ reviver: function(key, value) {
if ( typeof value === 'string' && value.length === 24) {
if (value.match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ$/)){
return new Date(value);
}
}
return value;
}}));
This will automatically converts all date strings to date objects when data is sent from client to server.
If you want to do the same thing for the Angular.js client, I found a good blog by Andrew Davey Automatic JSON date parsing with AngularJS
A node.js driver for MongoDB
v 1.4.31 fromChristian Amor Kvalheim
– Thirtieth