Bulk Update in RethinkDB -
i'm trying update multiple documents in rethinkdb, based on precalculated values in hash. i.e.
given table stats
primary key slug
data
[{slug: 'foo', stats: {}}, {slug:'bar', stats:{}}]
and given hash values
updated_stats = { 'foo' => {a: 1, b: 2}, 'bar' => {a: 3, b: 4} }
i can this
updated_stats.each{|k,v| r.table('stats').get(k).update{|s| { :stats => v } } }
so, why can't following?
r.table('stats').get_all(*updated_stats.keys).update{|s| { :stats => updated_stats[s["slug"]] } }
the rql shows nil value of updated_stats[s["slug"]]. appreciate on this. thanks.
it's tricky problem.
here's solution first.
r.table('stats').get_all(*updated_stats.keys).update{|s| { :stats => r.expr(updated_stats).get_field(s["slug"]) } }.run()
then updated_stats
ruby hash when use brackets, it's usual bracket operator, , since updated_stats
doesn't have key s["slug"], returns nil. have wrap updated_stats
in r.expr()
.
then brackets in ruby used nth
, get_field
, slice
etc. , when given variable, cannot guess 1 should use. have explicitly want use get_field
. add bracket term, should fix problem -- see https://github.com/rethinkdb/rethinkdb/issues/1179
sorry ran this!
Comments
Post a Comment