Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving a clientside date as UTC date object into Mongo

Tags:

mongodb

meteor

I am trying to save a date into meteor mongodb my challenge is as follows: 1) if i use new Date() it creates a date object in mongo DB however it saves the time as local time as javascript Date() this always comes with a timezone +0x:hours based on browser local timezone. When i retrieve this it causes havoc as i am assuming everything in my db is UTC.

2) I want to use moment js library which is great because it can represent dates in UTC properly but my challenge is how do i get mongo db to accept a moment time? The minute i use moment.format() it saves it as a string!

So how can i send a date to a mongodb insert command with a date object that is in UTC? string just dont work :(

Any help would be appreciated.

Thanks

like image 676
Piotr Stulinski Avatar asked Sep 11 '25 11:09

Piotr Stulinski


1 Answers

I think everything you need to know about both of these questions can be found here and here.

TLDR:

  • If you directly insert/update from the client you will store a timestamp based on the user's clock. It will still be stored as UTC, but you may or may not want to trust that the time is correct. I strongly suggest using a method for any db modifications which involve time so that the server's version of time will always be used.

  • Moment objects are not serializable to a format compatible with mongodb. Use a date object and format it on the client.

like image 104
David Weldon Avatar answered Sep 13 '25 03:09

David Weldon