{"id":4633,"date":"2016-07-04T15:07:01","date_gmt":"2016-07-04T15:07:01","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4633"},"modified":"2016-07-04T15:07:01","modified_gmt":"2016-07-04T15:07:01","slug":"rethinkdb-reduce-entire-table","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-reduce-entire-table\/","title":{"rendered":"RethinkDB reduce an entire table"},"content":{"rendered":"<p>If you want to &#8220;reduce&#8221; an entire table in RethinkDB (combine it all into one record), it does not seem to work directly, like you&#8217;d expect.<\/p>\n<p>For instance, this would theoretically count the number of rows:<\/p>\n<pre lang=\"javascript\">\nr.db('Watson')\n .table('description_s_TextGetRankedNamedEntities')\n .reduce(\n    function(left, right) {\n      return r.add(left, 1)\n    }).default(0)\n<\/pre>\n<p>The difficulty is that you&#8217;re turning objects into ints, which can&#8217;t be done in a single step.<\/p>\n<p>So, in this case you can first turn every row into the number &#8220;1&#8221;, and then the process works fine:<\/p>\n<pre lang=\"javascript\">\nr.db('Watson')\n .table('description_s_TextGetRankedNamedEntities')\n .map(\n   function(row) {\n     return 1; \n   }\n )\n .reduce(\n    function(left, right) {\n      return r.add(left, 1)\n    }).default(0)\n<\/pre>\n<p>Similarly, if you want to combine a bunch of arrays into one giant array, you would need to take a similar step (in this case selecting the array out of the table).<\/p>\n<pre lang=\"javascript\">\nr.db('Watson')\n .table('description_s_TextGetRankedNamedEntities')\n .map( \n   function(row) {\n     return row('entities');\n   }\n  ).reduce(\n    function(left, right) {\n      return r.add(left, right)\n    })\n<\/pre>\n<p>Depending on the scenario, you may also find that you want to &#8220;group&#8221; the rows in advance, and operate on the groups.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to &#8220;reduce&#8221; an entire table<\/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":[466],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4633"}],"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=4633"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4633\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}