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 times $reduce = "function(key, values) { ". "var total_count = 0;". "var total_resp_time = 0;". "values.forEach(function(doc) {". "total_count += doc.count;". "total_resp_time += doc.resp_time;". "});". "return {count: total_count...