title: Mongo Map Reduce first time
body: <p>First time Map/Reduce user here, and using MongoDB. I have a lot of page visit data which I'd like to make some sense of by using Map/Reduce. Below is basically what I want to do, but as a total beginner a Map/Reduce, I think this is above my knowledge!</p>
  
  <ol>
  <li>Go through all the pages with visits in the last 30 days, and where external = true.</li>
  <li>Then for each page, find all visits</li>
  <li>Group all visits by referral location</li>
  <li>For each referral location, calculate how many then went to visit a page which has a certain "type" and also has a certain word in the "tags".</li>
  </ol>
  
  <p>The database and collection are organised as </p>
  
  <pre><code>$mongo-&gt;dbname-&gt;visits
  </code></pre>
  
  <p>A sample document is:</p>
  
  <pre><code>{"url": "www.example.com", "type": "a", "refer": {"external": true, "domain": "twitter.com", "url": "http://www.twitter.com/page"}, "page": "1235", "user": "1232", "time": 1234567890}
  </code></pre>
  
  <p>And then I want to find documents of type B with a certain tag.</p>
  
  <pre><code>{"url": "www.example.com", "type": "b", "page": "745", "user": "1232", "time": 1234567890, "tags": {"a", "b", "c"}}
  </code></pre>
  
  <p>I'm using the normal Mongo PHP extension if that has an impact.</p>