In this example, we will run a MapReduce job that counts the frequency of tags in our sample collection. We will perform the operation within a PHP script and use the result to build a tag cloud using CSS and HTML:
1. Create a new script named
tagcloud.php
using your text editor and put the following code in it:<?php require('dbconnection.php'); $mongo = DBConnection::instantiate(); //get an instance of MongoDB object $db = $mongo->database; //define the map function $map = new MongoCode("function() {". "for (i = 0; i < this.tags.length; i++) {". "emit(this.tags[i], 1);". "}". "}"); //define the reduce function $reduce = new MongoCode("function(key, values) {". "var count = 0;". "for (var i = 0; i < values.length; i++){". "count += values[i];". "}". "return count;". "}"); //Run the map and reduce functions, store results in a collection //named tagcount $command = array( 'mapreduce' => 'sample_articles', 'map' => $map, 'reduce' =...