node.js - Converting a mongo stored date back into milliseconds since Unix epoch when loaded? -
i using mongoose & node.js webserver.
as part of 1 of document schemas, have 'timestamp' field. line in schema is: timestamp: { type: date, default: date.now }
this works fine, , allows me retrieve documents based on timestamp, however, saves isodate format described here: http://docs.mongodb.org/manual/core/document/#date, this:
"timestamp":"2013-04-04t19:31:38.514z"
i don't mind this, send client is. means have use date.parse() @ client end before can comparative operations it.
is there way either store date integer, or automatically convert 1 when it's retrieved?
is there reason should keep how is, , deal @ client end?
thanks in advance.
you can add numerical milliseconds version of timestamp
virtual attribute on schema:
schema.virtual('timestamp_ms').get(function() { return this.timestamp.gettime(); });
then can enable virtual field's inclusion in toobject
calls on model instances via option on schema:
var schema = new schema({ timestamp: date }, { toobject: { getters: true } });
Comments
Post a Comment