Mass update in RethinkDB

I am trying to update several documents in RethinkDB based on some pre-calculated values ​​in Hash. i.e.

For a table statswith a primary key slugwith data of type

[{slug: 'foo', stats: {}}, {slug:'bar', stats:{}}]

and a hash is set with values ​​such as

updated_stats = {
  'foo' => {a: 1, b: 2}, 
  'bar' => {a: 3, b: 4}
}

I can do it

updated_stats.each{|k,v| 
  r.table('stats').get(k).update{|s| 
    { :stats => v } 
  }  
}

So why can't I do the following?

r.table('stats').get_all(*updated_stats.keys).update{|s| 
  { :stats => updated_stats[s["slug"]] }  
}

rql shows nil as the value of updated_stats [s ["slug"]]. I would really appreciate any help with this. Thanks.

+4
source share
1 answer

This is a difficult problem.

Here is the first solution.

r.table('stats').get_all(*updated_stats.keys).update{|s| 
  { :stats => r.expr(updated_stats).get_field(s["slug"]) } 
}.run()

updated_stats ruby, , , , updated_stats s [ "slug" ], nil. updated_stats r.expr().

ruby ​​ nth, get_field, slice .. , , . , get_field. , - . https://github.com/rethinkdb/rethinkdb/issues/1179

, !

+5

Source: https://habr.com/ru/post/1524764/


All Articles