'Why are Date types stored as Time strings in MongoDB
Running a Rails app with MongoId 7.1 as ODM. Given this model:
class Memory
include Mongoid::Document
field :day_of_event, type: Date
end
Lets say, I create a memory:
m1 = Memory.create!(day_of_event: Date.new(2017,3,2))
If I enter into my console:
m1.day_of_event => Thu, 02 Mar 2017
m1.class => Date
BUT
m1.attributes['day_of_event'] => 2017-03-02 00:00:00 UTC
m1.attributes['day_of_event'].class => Time
m1.read_attribute_before_type_cast('day_of_event').class => Time
The attributes and read_attribute_before_type_cast method return raw_data (bypassing MongoId) which is recognised as Time type in Ruby. (strictly speaking, both should return strings from the db)
I want to store a date, not a point in time. I don't want to see the birthday of my nephew on a different day, because I didn't expect Timezone information in my data. I don't understand, why the UTC information is added here. Why are there Date and Timestamp types, if in the end both are stored as data containing time information?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
