问题涉及mongoid / moped DATE类型插入。 我的代码在
之下s=Moped::Session::new(["127.0.0.1"])
s.use "test"
s["a"].insert mydate: Date.strptime("10/02/2014","%m/%d/%Y")
引发错误
# => undefined method `__bson_dump__' for Thu, 02 Oct 2014:Date
为什么日期类型无法通过轻便摩托车插入mongoDB? 我很确定mongoDB确实支持Date类型。
感谢您的帮助。
答案 0 :(得分:2)
MongoDB支持BSON类型的UTC日期时间,在Moped中,它映射到Ruby Time,而不是Date。 但是,对于您的代码,有一个非常简单的解决方案,因为Mongoid提供了Date#mongoize便利功能。 希望这是你想要的,它有所帮助。
date_mongoize.rb
require 'moped'
require 'mongoid'
s=Moped::Session::new(["127.0.0.1"])
s.use "test"
s["a"].find.remove_all
s["a"].insert mydate: Date.strptime("10/02/2014","%m/%d/%Y").mongoize
p s["a"].find.to_a
$ ruby date_mongoize.rb
[{"_id"=>"5272a943fa23bace4f7650e3", "mydate"=>2014-10-02 00:00:00 UTC}]