Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two things about your question:</p> <p>1) The example in the cookbook might be a bit too complex for what you're trying to do. Here is a simpler one:</p> <p>Given a document structure that looks like this:</p> <pre><code>{ "url" : "http://example.com/page8580.html", "user_id" : "Jonathan.Clark", "date" : ISODate("2012-06-11T10:59:36.271Z") } </code></pre> <p>Here is some sample JavaScript code to run a map/reduce job that will count the number of visits per distinct URL.</p> <pre><code>// Map function: map = function() { emit({ url: this.url }, {count: 1}); } // Reduce function: reduce = function(key, values) { var count = 0; values.forEach( function(val) { count += val['count']; } ); return {count: count}; }; // Run the Map/Reduce function across the 'pageviews' collection: // Note that MongoDB will store the results in the 'pages_per_day' // collection because the 'out' parameter is present db.pageviews.mapReduce( map, // pass in the 'map' function as an argument reduce, // pass in the 'reduce' function as an argument // options { out: 'pages_per_day', // output collection verbose: true } // report extra statistics ); </code></pre> <p>2) If you want to run the Map/Reduce function on only a subset of the 'pageviews' collection, you can specify a query to the call to 'mapReduce()' in order to restrict the number of documents that the 'map()' function will operate on:</p> <pre><code>// Run the Map/Reduce function across the 'pageviews' collection, but // only report on the pages seen by "Jonathan.Clark" db.pageviews.mapReduce( map, // Use the same map &amp; reduce functions as before reduce, { out: 'pages_per_day_1user', // output to different collection query:{ 'user_id': "Jonathan.Clark" } // query descriptor verbose: true } ); </code></pre> <p>Note that if you aren't using JavaScript, you'll have to translate these calls into whatever programming language you're using.</p> <p>3) Here's an example of calling the Map/Reduce function with a query condition using PHP:</p> <pre><code>$outer = $this-&gt;cimongo-&gt;command (array ( "mapreduce" =&gt; "pageviews", "map" =&gt; $map, "reduce" =&gt; $reduce, "out" =&gt; "pages_per_day_1user", "query" =&gt; array( "user_id" =&gt; "Jonathan.Clark" ) )); </code></pre> <p>4) For more information about Map/Reduce, see the following references:</p> <ul> <li>Map/Reduce Manual page: <a href="http://www.mongodb.org/display/DOCS/MapReduce" rel="noreferrer">http://www.mongodb.org/display/DOCS/MapReduce</a></li> <li>Debugging Map/Reduce: <a href="http://www.mongodb.org/display/DOCS/Troubleshooting+MapReduce" rel="noreferrer">http://www.mongodb.org/display/DOCS/Troubleshooting+MapReduce</a></li> <li><p>Using Map/Reduce in PHP: <a href="http://us.php.net/manual/en/mongodb.command.php" rel="noreferrer">http://us.php.net/manual/en/mongodb.command.php</a></p> <p>--William </p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload