O'Reilly logo

PHP and MongoDB Web Development by Rubayeet Islam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - creating a tag cloud

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; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required