{"id":3232,"date":"2016-03-06T21:37:35","date_gmt":"2016-03-06T21:37:35","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3232"},"modified":"2016-03-06T21:37:35","modified_gmt":"2016-03-06T21:37:35","slug":"3232-2","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/3232-2\/","title":{"rendered":"RethinkDB: Insert Example"},"content":{"rendered":"<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n .insert({\n   'first_name': 'gary', \n   'last_name': 'sieling',\n   'user_name': 'gsieling'\n })\n<\/pre>\n<p>By default, this returns you the IDs you requested:<\/p>\n<pre lang=\"javascript\">\n{\n  \"deleted\": 0 ,\n  \"errors\": 0 ,\n  \"generated_keys\": [\n    \"903e0943-8ffe-41ca-a22f-7b1a964b399d\"\n  ] ,\n  \"inserted\": 1 ,\n  \"replaced\": 0 ,\n  \"skipped\": 0 ,\n  \"unchanged\": 0\n}\n<\/pre>\n<p>If you request the changes back, this will return you the values you inserted, in addition to the data inserted:<\/p>\n<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n .insert({\n   'first_name': 'gary', \n   'last_name': 'sieling',\n   'user_name': 'gsieling'\n }, {returnChanges: true})\n<\/pre>\n<p>This is what you get:<\/p>\n<pre lang=\"javascript\">\n{\n  \"changes\": [\n    {\n      \"new_val\": {\n        \"first_name\":  \"gary\" ,\n        \"id\":  \"947a3ce6-d419-4d31-bd1e-874c721a9e6c\" ,\n        \"last_name\":  \"sieling\" ,\n        \"user_name\":  \"gsieling\"\n      } ,\n      \"old_val\": null\n    }\n  ] ,\n  \"deleted\": 0 ,\n  \"errors\": 0 ,\n  \"generated_keys\": [\n    \"947a3ce6-d419-4d31-bd1e-874c721a9e6c\"\n  ],\n  \"inserted\": 1 ,\n  \"replaced\": 0 ,\n  \"skipped\": 0 ,\n  \"unchanged\": 0\n}\n<\/pre>\n<p>By default this sets durability to &#8220;hard&#8221; and doesn&#8217;t return the results, which should be a little faster. You can set durability to &#8220;soft&#8221; and it will insert a little faster, but risk losing data in a crash scenario:<\/p>\n<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n .insert({\n   'first_name': 'gary', \n   'last_name': 'sieling',\n   'user_name': 'gsieling'\n }, \n {\n    durability: 'soft'\n })\n<\/pre>\n<p>If instead of an insert, you want to replace an existing object, you can use &#8216;replace&#8217;:<\/p>\n<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n  .get('1b223750-799d-402a-b378-74c993a9c10c')\n  .replace(\n    {\n       user_name: 'test', \n       last_name: 'test', \n       first_name: 'test', \n       id: '1b223750-799d-402a-b378-74c993a9c10c'\n    })\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>r.db(&#8216;test&#8217;) .table(&#8216;users&#8217;) .insert({ &#8216;first_name&#8217;: &#8216;gary&#8217;, &#8216;last_name&#8217;: &#8216;sieling&#8217;, &#8216;user_name&#8217;: &#8216;gsieling&#8217; }) By default, this returns you the IDs you requested: { &#8220;deleted&#8221;: 0 , &#8220;errors&#8221;: 0 , &#8220;generated_keys&#8221;: [ &#8220;903e0943-8ffe-41ca-a22f-7b1a964b399d&#8221; ] , &#8220;inserted&#8221;: 1 , &#8220;replaced&#8221;: 0 , &#8220;skipped&#8221;: 0 , &#8220;unchanged&#8221;: 0 } If you request the changes back, this will return you the values &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/3232-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RethinkDB: Insert 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,466,523],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3232"}],"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=3232"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3232\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}