4

I was expecting that an object with a Date property would be saved in Mongo as an ISODate from both client or server side, but this is not the case.

When I do

if (Meteor.is_client()){
    Collection.insert({text : "Client", number : 1, date : new Date() });
    Collection.insert({text : "Client", number : 2, date : (new Date()).getTime() });
}
else {
    Collection.insert({text : "Server", number : 1, date : new Date() });
}

In mongo it saves like so

{_id : "xx-xx-xx-xx-xx", text : "Client", number : 1, date : "2012-08-21T18:40:47.446" }
{_id : "xx-xx-xx-xx-xx", text : "Client", number : 2, date : 1345574805367 }
{_id : "xx-xx-xx-xx-xx", text : "Server", number : 1, date : ISODate(2012-08-21T18:40:47.446) 

Is there a way to save an object with a Date property from client side as an ISODate?

Rui Gonçalves
  • 1,164
  • 8
  • 15
  • I'd consider this as a bug in meteor or perhaps another component that meteor uses. Have logged an issue for it: https://github.com/meteor/meteor/issues/603 – Tim Haines Jan 11 '13 at 23:08

1 Answers1

4

For me, I don't send timestamps from client side. Instead, I modify the document when inserting through Collection.allow function under auth branch.

I think there're several benefits to do this -

  • Client-side does not need to insert date field, which saves code.

  • The timestamp is based on the server time, rather than client-side, which should be more accurate.

  • And last, the field value is ISODate, rather than string. (Hate JSON without native date type supported)

jifeng.yin
  • 2,081
  • 1
  • 21
  • 19
  • Yes, all the reasons you gave are correct, I implemented on the server a hook so every time an insert or update request occurs for those collections the date_created and date_modified are added to the object. But in this case I wanted to implement client side. – Rui Gonçalves Aug 22 '12 at 12:02
  • 1
    You'd be better off using deny as it's always run. There can be multiple allow callbacks, and if one returns true, the other allow callbacks aren't run, which means your code might start getting mysteriously skipped. Using deny is a bit of a hack anyway, and a better solution will be forthcoming: https://github.com/meteor/meteor/issues/575 – Tim Haines Jan 11 '13 at 23:07