Jelajahi Sumber

Start stabilizing MongoCollection

Andreas Braun 10 tahun lalu
induk
melakukan
9e81cea007

+ 33 - 24
lib/Mongo/MongoCollection.php

@@ -144,19 +144,23 @@ class MongoCollection
             $options = $op;
         }
 
-        $command = [
-            'aggregate' => $this->name,
-            'pipeline' => $pipeline
-        ];
+        if (isset($options['cursor'])) {
+            $options['useCursor'] = true;
 
-        $command += $options;
+            if (isset($options['cursor']['batchSize'])) {
+                $options['batchSize'] = $options['cursor']['batchSize'];
+            }
 
-        try {
-            return $this->db->command($command);
-        } catch (MongoCursorTimeoutException $e) {
-            throw new MongoExecutionTimeoutException($e->getMessage(), $e->getCode(), $e);
+            unset($options['cursor']);
+        } else {
+            $options['useCursor'] = false;
         }
 
+        try {
+            return $this->collection->aggregate(TypeConverter::fromLegacy($pipeline), $options);
+        } catch (\MongoDB\Driver\Exception\Exception $e) {
+            throw ExceptionConverter::toLegacy($e);
+        }
     }
 
     /**
@@ -340,6 +344,7 @@ class MongoCollection
         }
 
         return [
+            'ok' => 1.0,
             'connectionId' => 0,
             'n' => 0,
             'syncMillis' => 0,
@@ -559,7 +564,6 @@ class MongoCollection
         $indexOptions = array_intersect_key($options, $neededOptions);
         $indexes = $this->collection->listIndexes();
         foreach ($indexes as $index) {
-
             if (! empty($options['name']) && $index->getName() === $options['name']) {
                 throw new \MongoResultException(sprintf('index with name: %s already exists', $index->getName()));
             }
@@ -707,22 +711,27 @@ class MongoCollection
                 TypeConverter::fromLegacy($document),
                 $this->convertWriteConcernOptions($options)
             );
-        } catch (\MongoDB\Driver\Exception\Exception $e) {
-            ExceptionConverter::toLegacy($e);
-        }
 
-        if (!$result->isAcknowledged()) {
-            return true;
-        }
+            if (! $result->isAcknowledged()) {
+                return true;
+            }
 
-        return [
-            'ok' => 1.0,
-            'nModified' => $result->getModifiedCount(),
-            'n' => $result->getMatchedCount(),
-            'err' => null,
-            'errmsg' => null,
-            'updatedExisting' => $result->getUpsertedCount() == 0,
-        ];
+            $resultArray = [
+                'ok' => 1.0,
+                'nModified' => $result->getModifiedCount(),
+                'n' => $result->getUpsertedCount() + $result->getModifiedCount(),
+                'err' => null,
+                'errmsg' => null,
+                'updatedExisting' => $result->getUpsertedCount() == 0,
+            ];
+            if ($result->getUpsertedId() !== null) {
+                $resultArray['upserted'] = TypeConverter::toLegacy($result->getUpsertedId());
+            }
+
+            return $resultArray;
+        } catch (\MongoDB\Driver\Exception\Exception $e) {
+            throw ExceptionConverter::toLegacy($e);
+        }
     }
 
     /**

+ 1 - 1
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -176,7 +176,7 @@ class MongoCollectionTest extends TestCase
 
     public function testBatchInsertException()
     {
-        $this->setExpectedException('MongoResultException', 'localhost:27017: cannot use \'w\' > 1 when a host is not replicated');
+        $this->setExpectedException('MongoResultException', 'cannot use \'w\' > 1 when a host is not replicated');
 
         $documents = [['foo' => 'bar']];
         $this->getCollection()->batchInsert($documents, ['w' => 2]);