How to store UTC time values ​​in Mongo with Mongoid?

The behavior I observe with the Mongoid adapter is that it will save the "time" fields with the current system time zone in the database. Please note that this is the system time, not the Rail.zone environment. If I change the time zone of the system, subsequent saves will display the current time zone of the system.

# system currently at UTC -7 @record.time_attribute = Time.now.utc @record.save # in mongo, the value is "time_attribute" : "Mon May 17 2010 12:00:00 GMT-0700 (QYZST)" @record.reload.time_attribute.utc? # false 
+4
source share
1 answer

Try setting the use_utc mongoid configuration parameter to true .

He tells Mongoid that you want to return the time in UTC: http://github.com/durran/mongoid/blob/master/lib/mongoid/config.rb#L22

+1
source

Source: https://habr.com/ru/post/1310064/


All Articles