{"id":3402,"date":"2016-03-15T02:42:45","date_gmt":"2016-03-15T02:42:45","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3402"},"modified":"2016-03-15T02:42:45","modified_gmt":"2016-03-15T02:42:45","slug":"rethinkdb-coalesce-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-coalesce-example\/","title":{"rendered":"RethinkDB: coalesce example"},"content":{"rendered":"<p>In SQL, the coalesce or NVL function takes null values and replaces them with a defined value.<\/p>\n<p>To replicate this in RethinkDB, you can use the &#8216;branch&#8217; function to test for null and remove it:<\/p>\n<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n .map(\n   (doc) => { \n     return\n       {\n         first_name: null\n       }\n   }\n ).map( \n   (doc) => {\n     return {\n       first_name: \n         r.branch(\n           doc('first_name').eq(null), \n           'no name', \n           doc('first_name')  \n         )\n     }\n   }\n )\n<\/pre>\n<p>If you are stuck instead with undefined values, you can instead merge in values for these columns:<\/p>\n<pre lang=\"javascript\">\nr.db('test').table('users')\n  .map(\n    (doc) => { return r.expr(\n      {\n        first_name: '',\n        last_name: ''\n      }\n    ).merge(doc) }\n  )\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In SQL, the coalesce or NVL function takes null values and replaces them with a defined value. To replicate this in RethinkDB, you can use the &#8216;branch&#8217; function to test for null and remove it: r.db(&#8216;test&#8217;) .table(&#8216;users&#8217;) .map( (doc) => { return { first_name: null } } ).map( (doc) => { return { first_name: r.branch( &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-coalesce-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RethinkDB: coalesce 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\/3402"}],"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=3402"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3402\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}