Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host="localhost:27017";
- $userdb="WestCrime";
- $database=$userdb.".crime"; /* this is the database with collection */
- $manager = new MongoDB\Driver\Manager("mongodb://{$host}/{$userdb}");
- $filter = [];
- $options = ['sort' => ['count' => -1], 'limit' => 5];
- if ($manager) {
- $cmd = new MongoDB\Driver\Command( [ 'aggregate' => 'crime',
- 'pipeline' => [
- ['$match' => ['Crime type' =>'Vehicle crime']],
- ['$group' => ['_id'=>['lsoa'=>'$LSOA code', 'type'=>'$Crime type', 'count'=>['$sum' => 1]]]],
- ['$sort' => ['count' => -1]],
- //['$limit' => 1],
- ],
- "explain" => False
- ]);
- $r = $manager->executeCommand( $userdb, $cmd );
- $array = json_decode(json_encode($r),true);
- foreach($r as $document) {
- echo '"_id": '.$document["_id"]."<br />";
- echo '"title": '.$document["title"]."<br />";
- echo '"author": '.$document["author"]."<br />";
- echo '***************************************';
- }
- }else{
- echo "connection failed";}
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement