{"id":3286,"date":"2016-03-07T03:09:43","date_gmt":"2016-03-07T03:09:43","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3286"},"modified":"2016-03-07T03:09:43","modified_gmt":"2016-03-07T03:09:43","slug":"rethinkdb-union-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-union-example\/","title":{"rendered":"RethinkDB: UNION ALL Example"},"content":{"rendered":"<p>Doing a &#8220;UNION ALL&#8221; in RethinkDB is fairly simple: you take a dataset, and add a &#8220;union&#8221; predicate to the end, with another dataset:<\/p>\n<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n .filter( \n    (x) => x('user_name').match('test ') \n  )\n .union(\n   r.db('test')\n    .table('users')\n    .filter(\n      (x) => x('user_name').match('9')\n    )\n )\n<\/pre>\n<p>It&#8217;s worth noting that this is very different from SQL: rows are not de-duplicated. Syntactically, the two datasets live at the same level in SQL, whereas in this code example, the second dataset is nested.<\/p>\n<p>If you want this to behave like a normal UNION, add a DISTINCT:<\/p>\n<pre lang=\"javascript\">\nr.db('test')\n .table('users')\n .filter( (x) => x('user_name').match('test ') )\n .union(\n   r.db('test')\n    .table('users')\n    .filter(\n      (x) => x('user_name').match('9')\n    )\n )\n .distinct()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Doing a &#8220;UNION ALL&#8221; in RethinkDB is fairly simple: you take a dataset, and add a &#8220;union&#8221; predicate to the end, with another dataset: r.db(&#8216;test&#8217;) .table(&#8216;users&#8217;) .filter( (x) => x(&#8216;user_name&#8217;).match(&#8216;test &#8216;) ) .union( r.db(&#8216;test&#8217;) .table(&#8216;users&#8217;) .filter( (x) => x(&#8216;user_name&#8217;).match(&#8216;9&#8242;) ) ) It&#8217;s worth noting that this is very different from SQL: rows are not de-duplicated. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-union-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RethinkDB: UNION ALL 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\/3286"}],"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=3286"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3286\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}