我使用Ruby在MongoDB中的查询输出:
irb(main):087:0> data = col.find({}, :fields => ["result", "time"])
=> <Mongo::Cursor:0x14768b8 namespace='spark.light' @selector={} @cursor_id=>
irb(main):090:0> data.first
=> {"_id"=>BSON::ObjectId('537d961197c20960ad000001'), "result"=>2177, "time"=>2014-05-22 06:15:45 UTC}
现在我想将这些数据提供给在Ruby on Rails上运行的chartkick来绘制线图 。输入应如下所示:
"2014-05-22 06:15:45 UTC" => "2177"
有没有干净的方法呢?
答案 0 :(得分:2)
这会将记录转换为time => result
的哈希值:
Hash[data.map do |item|
[item['time'], item['result']]
end]
# => { 2014-05-22 06:15:45 UTC => 2177, 2014-05-22 06:20:00 UTC => 1000 }
答案 1 :(得分:0)
Hash[data.first.values_at('time', 'result')]