Reputation: 173
I have this output from a query in MongoDB using Ruby :
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}
Now I want to give this data to chartkick running on Ruby on Rails to draw a linechart .The input shall looks like this :
"2014-05-22 06:15:45 UTC" => "2177"
Is there any clean way to do that ?
Upvotes: 1
Views: 367
Reputation: 118271
Use Hash#values_at
with Hash::[]
method :
Hash[data.first.values_at('time', 'result')]
Upvotes: 0
Reputation: 4570
This would transform records into a hash of 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 }
Upvotes: 2