/**
  * @return ArrayIterator
  * @throws \Exception|\MongoException
  * @throws \RuntimeException
  */
 public function aggregate()
 {
     $database = $this->collection->getDatabase();
     $command = array();
     $command['aggregate'] = $this->collection->getName();
     $command['pipeline'] = $this->getPipeline();
     try {
         $result = $database->command($command);
     } catch (\MongoException $e) {
         throw $e;
     }
     if (!$result['ok']) {
         throw new \RuntimeException($result['errmsg']);
     }
     return new ArrayIterator(isset($result['result']) ? $result['result'] : array());
 }
 /**
  * Normalize products into their MongoDB document representation
  *
  * @param ProductInterface[] $products
  *
  * @return array
  */
 protected function getDocsFromProducts(array $products)
 {
     $context = [ProductNormalizer::MONGO_COLLECTION_NAME => $this->collection->getName()];
     $docs = [];
     foreach ($products as $product) {
         $docs[] = $this->normalizer->normalize($product, ProductNormalizer::FORMAT, $context);
     }
     return $docs;
 }
 /**
  * @return array
  *
  * @throws \Exception
  */
 protected function getCollectionStats()
 {
     $database = $this->collection->getDatabase();
     $name = $this->collection->getName();
     try {
         if (!($data = $database->command(array('collStats' => $name)))) {
             $message = sprintf("Statistics not found for collection %s", $name);
             throw new Exception($message);
         }
         if (isset($data['errmsg'])) {
             throw new Exception($data['errmsg']);
         }
         return $data;
     } catch (Exception $e) {
         throw $e;
     }
     return array();
 }