Advertisement
Guest User

Untitled

a guest
May 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. $host="localhost:27017";
  4. $userdb="WestCrime";
  5.      $database=$userdb.".crime";   /* this is the database with collection */
  6.        $manager = new MongoDB\Driver\Manager("mongodb://{$host}/{$userdb}");
  7.        $filter = [];
  8.        $options = ['sort' => ['count' => -1], 'limit' => 5];
  9. if ($manager) {
  10.            
  11.            
  12.            $cmd = new MongoDB\Driver\Command( [ 'aggregate' => 'crime',
  13.                                         'pipeline' => [
  14.                                                                  ['$match' => ['Crime type' =>'Vehicle crime']],
  15.                                                                  ['$group' => ['_id'=>['lsoa'=>'$LSOA code', 'type'=>'$Crime type', 'count'=>['$sum' => 1]]]],
  16.                                                                  ['$sort' => ['count' => -1]],
  17.                                                                  //['$limit' => 1],
  18.                                                                                                                                            
  19.                                                     ],
  20.                                                     "explain" => False
  21.                                                     ]);
  22.            $r = $manager->executeCommand( $userdb, $cmd );
  23.            $array = json_decode(json_encode($r),true);
  24.            foreach($r as $document) {
  25.                 echo '"_id": '.$document["_id"]."<br />";
  26.                 echo '"title": '.$document["title"]."<br />";
  27.                 echo '"author": '.$document["author"]."<br />";
  28.                 echo '***************************************';
  29.             }
  30.  
  31.         }else{
  32.         echo "connection failed";}
  33.  
  34.         ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement