MongoDB - Storing date without timezone

后端 未结 2 1450
北海茫月
北海茫月 2021-01-18 16:01

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.

Re

2条回答
  •  情歌与酒
    2021-01-18 16:52

    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/

提交回复
热议问题