{"id":1300,"date":"2022-08-30T15:15:23","date_gmt":"2022-08-30T15:15:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/mongodb-javascript-pass-argument-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:23","modified_gmt":"2022-08-30T15:15:23","slug":"mongodb-javascript-pass-argument-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/mongodb-javascript-pass-argument-collection-of-common-programming-errors\/","title":{"rendered":"MongoDB javascript pass argument-Collection of common programming errors"},"content":{"rendered":"<p>This line:<\/p>\n<pre><code>db.smsdb.mapReduce(map(2), reduce, \"collection\")\n<\/code><\/pre>\n<p>is calling <code>map(2)<\/code> with the result (<code>undefined<\/code>) being passed as the map function for <code>mapReduce<\/code>.<\/p>\n<p>Instead do something like this:<\/p>\n<pre><code>db.smsdb.mapReduce(function(){ map.call(this, 2); }, reduce, \"collection\")\n<\/code><\/pre>\n<p><strong>UPDATE<\/strong><\/p>\n<p>The above doesn&#8217;t work because the <code>map<\/code> function isn&#8217;t available in the scope the <code>mapReduce<\/code> map function is run. So you have to wrap it up into a single function that can generate the map function you need:<\/p>\n<pre><code>var mapper = function(n) {\n    function map() {\n        if(this.x == n){\n            emit(this.x);\n        }\n    }\n    return map;\n};\ndb.smsdb.mapReduce(mapper(2), reduce, \"collection\");\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:15:20. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This line: db.smsdb.mapReduce(map(2), reduce, &#8220;collection&#8221;) is calling map(2) with the result (undefined) being passed as the map function for mapReduce. Instead do something like this: db.smsdb.mapReduce(function(){ map.call(this, 2); }, reduce, &#8220;collection&#8221;) UPDATE The above doesn&#8217;t work because the map function isn&#8217;t available in the scope the mapReduce map function is run. So you have to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1300","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1300"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1300\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}