如何使用Mongoid在Mongo中存储UTC时间值?

时间:2010-05-18 00:45:18

标签: ruby-on-rails mongodb mongoid

我使用Mongoid适配器观察的行为是它将使用当前系统时区的“时间”字段保存到数据库中。请注意,这是系统时间而不是Rail的环境Time.zone。如果我更改系统时区,则后续保存将获取当前系统时区。

# 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

1 个答案:

答案 0 :(得分:1)

尝试将use_utc mongoid配置参数设置为true

它告诉Mongoid您希望以UTC格式返回时间: http://github.com/durran/mongoid/blob/master/lib/mongoid/config.rb#L22