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 - finding total views and average response time per blog post

In this example, we will write a PHP program where we will define the map and reduce functions to determine the number of visits per blog post over the last seven days and the average response time for rendering it. We will display the result in an HTML table:

  1. Create a new PHP script named page_views.php. Add the following code to it:
    <?php require 'dbconnection.php'; $dbConnection = DBConnection::instantiate(); $db = $dbConnection->database; /* The map function, emits a counter and response_time_ms of each document */ $map = "function() { emit(this.query_params.id, {count: 1,". "resp_time: this.response_time_ms}) }"; // the reduce function, sums up the counters and response ...

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