What is the best way to store dates in MongoDB? -
i starting learn mongodb , hoping migrate mysql.
in mysql, there 2 different data types - date ('0000-00-00')
, datetime ('0000-00-00 00:00:00')
. in mysql, use date
type, not sure how transfer them mongodb. in mongodb, there date
object, comparable datetime
. seems appropriate use date
objects, wasting space, since hours, min, sec not utilized. on other hand, storing dates strings seems wrong.
is there golden standard on storing dates ('0000-00-00')
in mongodb?
bson (the storage data format used mongo natively) has dedicated date type utc datetime 64 bit (so, 8 byte) signed integer denoting milliseconds since unix time epoch. there few valid reasons why use other type storing dates , timestamps.
if you're desperate save few bytes per date (again, mongo's padding , minimum block size , worth trouble in rare cases) can store dates 3 byte binary blob storing unsigned integer in yyyymmdd format, or 2 byte binary blob denoting "days since january 1st of year x" x must chosen appropriately since supports date range spanning 179 years.
edit: discussion below demonstrates viable approach in rare circumstances. basically; use mongo's native date type ;)
Comments
Post a Comment