{"id":3393,"date":"2016-03-15T01:36:03","date_gmt":"2016-03-15T01:36:03","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3393"},"modified":"2016-03-15T01:36:03","modified_gmt":"2016-03-15T01:36:03","slug":"rethinkdb-upsert-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-upsert-example\/","title":{"rendered":"RethinkDB: Upsert Example"},"content":{"rendered":"<p>To create a test case, we&#8217;ll make a table:<\/p>\n<pre lang=\"javascript\">r.db('test').table('users').insert([{\n  first_name: 'gary',\n  last_name: 'sieling'\n},\n{\n  first_name: 'melissa'\n},\n{\n  last_name: 'a test'\n}])\n<\/pre>\n<div id=\"sticky-wrap\">\n<div id=\"main-container\">\n<div id=\"cluster\" class=\"container\">\n<div id=\"dataexplorer\">\n<div class=\"section\">\n<div class=\"results_container\">\n<div class=\"result_view\">\n<div class=\"results_header\">\n<p class=\"metadata  feed_paused\">3 rows returned. Displaying rows 1-3<\/p>\n<\/div>\n<div class=\"tab-content\">\n<div class=\"results table_view_container\">\n<div class=\"results_wrap table_view\">\n<div class=\"json_table_container\">\n<table class=\"json_table\">\n<tbody>\n<tr class=\"jta_tr\">\n<td class=\"td_attr col-record col-record-attr\" data-col=\"record\"><\/td>\n<td class=\"td_attr col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-0\">id<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-1\" data-col=\"1\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-1\">first_name<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-2\">last_name<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr class=\"jta_tr\">\n<td class=\"col-record-td\" data-col=\"record\">\n<div class=\"jta_value col-record\">1<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_string\">46454a3e-ba9e-4991-bc7f-53f1e5c19906<\/div>\n<\/div>\n<\/td>\n<td class=\"col-1\" data-col=\"1\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-1 jta_string\">gary<\/div>\n<\/div>\n<\/td>\n<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_string\">sieling<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr class=\"jta_tr\">\n<td class=\"col-record-td\" data-col=\"record\">\n<div class=\"jta_value col-record\">2<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_string\">450e77d6-d5c6-4533-b2d7-c0158bb097c9<\/div>\n<\/div>\n<\/td>\n<td class=\"col-1\" data-col=\"1\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-1 jta_string\">melissa<\/div>\n<\/div>\n<\/td>\n<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr class=\"jta_tr\">\n<td class=\"col-record-td\" data-col=\"record\">\n<div class=\"jta_value col-record\">3<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_string\">e08b00c8-b8d2-465b-9886-ee014d14e751<\/div>\n<\/div>\n<\/td>\n<td class=\"col-1\" data-col=\"1\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-1 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_string\">a test<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"footer\"><\/div>\n<p>If you try to re-insert these, you&#8217;ll get a duplicate key violation:<\/p>\n<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n .insert({\n  id: '46454a3e-ba9e-4991-bc7f-53f1e5c19906',\n  first_name: 'test upsert 1',\n  last_name: 'test upsert 2'\n})\n<\/pre>\n<pre>\nDuplicate primary key `id`:\n{\n  \"first_name\":\t\"gary\",\n  \"id\":\t\"46454a3e-ba9e-4991-bc7f-53f1e5c19906\",\n  \"last_name\":\t\"sieling\"\n}\n{\n  \"first_name\":\t\"test upsert 1\",\n  \"id\":\t\"46454a3e-ba9e-4991-bc7f-53f1e5c19906\",\n  \"last_name\":\t\"test upsert 2\"\n}\n<\/pre>\n<p>If you want to do an upsert, you can choose to either replace the entire object, or just matching keys.<\/p>\n<p>To replace the entire object:<\/p>\n<pre lang=\"javascript\">\nr.db('test').table('users').insert({\n  id: '46454a3e-ba9e-4991-bc7f-53f1e5c19906',\n  first_name: 'test upsert 1',\n  last_name: 'test upsert 2'\n}, {\n  conflict: 'replace'\n})\n<\/pre>\n<p>If you want to merge objects, you can do it with &#8216;update&#8217;:<\/p>\n<pre lang=\"javascript\">\nr.db('test').table('users').insert({\n  id: '46454a3e-ba9e-4991-bc7f-53f1e5c19906',\n  first_name: 'Gary'\n}, {\n  conflict: 'update'\n})\n<\/pre>\n<p>You can also use this to add new attributes to existing objects.<\/p>\n<p>Note that you can&#8217;t replace pieces of the object this way, as they will get replaced in their entirety:<\/p>\n<pre lang=\"javascript\">\nr.db('test').table('users').insert({\n  id: '46454a3e-ba9e-4991-bc7f-53f1e5c19906',\n  work_history: [\n    {title: 'Job 1'},\n    {title: 'Job 2'}\n  ]  \n}, {\n  conflict: 'update'\n});\n\nr.db('test').table('users').insert({\n  id: '46454a3e-ba9e-4991-bc7f-53f1e5c19906',\n  work_history: [\n    {employer: 'Employer 1'},\n    {title: 'Employer 2'}\n  ]  \n}, {\n  conflict: 'update'\n})\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To create a test case, we&#8217;ll make a table: r.db(&#8216;test&#8217;).table(&#8216;users&#8217;).insert([{ first_name: &#8216;gary&#8217;, last_name: &#8216;sieling&#8217; }, { first_name: &#8216;melissa&#8217; }, { last_name: &#8216;a test&#8217; }]) 3 rows returned. Displaying rows 1-3 id first_name last_name 1 46454a3e-ba9e-4991-bc7f-53f1e5c19906 gary sieling 2 450e77d6-d5c6-4533-b2d7-c0158bb097c9 melissa undefined 3 e08b00c8-b8d2-465b-9886-ee014d14e751 undefined a test If you try to re-insert these, you&#8217;ll get a &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-upsert-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RethinkDB: Upsert Example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[160,462,466,523],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3393"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3393"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3393\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}