Execute an aggregation pipeline command and retrieve results through a cursor
public aggregateCursor ( array $pipeline, array $options = [] ) : MongoCommandCursor | ||
$pipeline | array | |
$options | array | |
return | MongoCommandCursor |
function mkonereg($db, $colname, $grouparr) { $ops = array(); // $ops[] = ['$match' => $query]; // $ops[] = ['$sort' => ['year'=> -1, 'month'=> -1]]; $ops[] = ['$group' => $grouparr]; $option = ['allowDiskUse' => true]; //print_r($ops); $collection = new MongoCollection($db, $colname); echo "working on: {$colname} ... with"; $col2name = $colname . "_reg"; $col2 = new MongoCollection($db, $col2name); print_r($grouparr['_id']); makegrpIndex($db, $col2, $grouparr['_id']); //print_r($ops); try { $cursor = $collection->aggregateCursor($ops, $option); } catch (MongoException $e) { echo "error message: " . $e->getMessage() . "\n"; echo "error code: " . $e->getCode() . "\n"; exit(1); } //$results = $cursor['result']; foreach ($cursor as $result) { //print_r($result[_id]); $col2->insert($result['_id']); } }
/** * Convert foreign id values from MongoId to string. * * @return void */ public function down() { // The Client model has a mutator that converts lrs_id from string to MongoId, // so the Mongo classes are used to directly modify the client collection. $db = \DB::getMongoDB(); $clients = new MongoCollection($db, 'client'); $lrsIds = $clients->aggregateCursor([['$group' => ['_id' => '$lrs_id']]]); foreach ($lrsIds as $lrsId) { $clients->update(['lrs_id' => $lrsId['_id']], ['$set' => ['lrs_id' => (string) $lrsId['_id']]], ['multiple' => true]); } echo 'Foreign id values in client collection converted from MongoId to string.' . PHP_EOL; }
/** * Retrieves an iterator on a list of abusive Commentary * * @param int $offset * @param int $limit * * @return \MongoCursor */ public function findMostReportedCommentary($offset = 0, $limit = 20) { return $this->collection->aggregateCursor([['$match' => ['commentary.0' => ['$exists' => true], 'commentary' => ['$elemMatch' => ['abusiveCount' => ['$gt' => 0]]]]], ['$unwind' => '$commentary'], ['$match' => ['commentary.abusiveCount' => ['$gt' => 0]]], ['$project' => ['commentary' => true]], ['$sort' => ['commentary.abusiveCount' => -1]]]); // I love arrays }