{"id":3399,"date":"2016-03-15T02:35:44","date_gmt":"2016-03-15T02:35:44","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3399"},"modified":"2016-03-15T02:35:44","modified_gmt":"2016-03-15T02:35:44","slug":"rethinkdb-case-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-case-example\/","title":{"rendered":"RethinkDB: CASE WHEN example"},"content":{"rendered":"<p>There are a couple ways to mimic the SQL &#8220;CASE WHEN&#8221; in RethinkDB.<\/p>\n<p>If the rows don&#8217;t have values in some columns, in SQL you&#8217;d default these with &#8220;CASE WHEN attr IS NULL &#8230; ELSE END&#8221;. To fix these, you can use map \/ merge: <\/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<p>If you want to key off different values, you can use the &#8216;branch&#8217; function, like so:<\/p>\n<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n .map( \n   (doc) => {\n     return {\n       first_name: \n         r.branch(\n           doc('first_name').eq('test'), \n           1, \n           2)  \n     }\n   }\n  )\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are a couple ways to mimic the SQL &#8220;CASE WHEN&#8221; in RethinkDB. If the rows don&#8217;t have values in some columns, in SQL you&#8217;d default these with &#8220;CASE WHEN attr IS NULL &#8230; ELSE END&#8221;. To fix these, you can use map \/ merge: r.db(&#8216;test&#8217;).table(&#8216;users&#8217;) .map( (doc) => { return r.expr( { first_name: &#8221;, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-case-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RethinkDB: CASE WHEN 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\/3399"}],"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=3399"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3399\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}