Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - Storing date without timezone

We have a simple application in which we have all user in same timezone & therefore we are not interested to store timezone information in mongo date object.

Reason for such extreme step is we have multiple micro service using common database managed by different developers. Each of them requires to explicitly set timezone related stuff in query & forgetting same results in invalid dataset.

Since currently MongoDB folks Mongo Data Types

doesn't support storing dates without timezone.

Just eager to know that is their any alternative approach to represent date without timezone in mongo by which we can still able to take advantage of mongo database queries date based syntax like date ranges, date etc.

At the same time it would be convenient for DBA's to read and manage records.

like image 386
tarunkumar Avatar asked Jul 15 '16 16:07

tarunkumar


1 Answers

Look at this answer: https://stackoverflow.com/a/6776273/6105830

You can use two types of long representation (milliseconds or format yyyyMMddHHmmss). These are the only ways to not store timezone and still be able to make range queries.

Unfortunately you lost some aggregation properties. But you can do something like keeping two representations and use them at opportune times.

UPDATE:

Do not store date as I said before. You will lost many and many features of MongoDB and also will be hard to perform major operators on date fields.

Newer versions of MongoDB has operators to deal with timezone, and it should be enough to work with ISOTime formats. My application was using my own suggestion to store date. Now I have to let my users select their TimeZone (company has grown and we need to expand to other countries). We are struggling to change all models to use timestamp instead of a normalized date format. For further more explore the link: https://docs.mongodb.com/manual/reference/method/Date/

and you can also use MongoDB official community channel for questioning Here is the link: https://developer.mongodb.com/community/forums/

like image 188
Lucas Soares Avatar answered Sep 19 '22 01:09

Lucas Soares