In this example, we will list the distinct categories of the article objects in the sample_articles
collection by invoking the distinct()
method from a PHP program:
1. Create a new PHP script named
distinct.php
and put the following code in it:<?php require('dbconnection.php'); $mongo = DBConnection::instantiate(); //get an instance of MongoDB object $db = $mongo->database; $result = $db->command(array('distinct' => 'sample_articles', 'key' => 'category')); ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Categories</title> <link rel="stylesheet" href="style.css"/> </head> <body> <div id="contentarea"> <div id="innercontentarea"> <h1>Distinct Categories</h1> <ul> <?php foreach($result['values'] as $value): ?> <li><?php echo $value; ?></li> <?php endforeach; ?> </ul> </div>...