Przeglądaj źródła

Merge pull request #62 from alcaeus/remove-index-comparison

Remove index comparison
Andreas 10 lat temu
rodzic
commit
b16e5a7157
1 zmienionych plików z 17 dodań i 22 usunięć
  1. 17 22
      lib/Mongo/MongoCollection.php

+ 17 - 22
lib/Mongo/MongoCollection.php

@@ -572,12 +572,10 @@ class MongoCollection
             $options['name'] = \MongoDB\generate_index_name($keys);
         }
 
-        // duplicate
-        $skippedOptions = ['background' => 1, 'dropDups' => 1, 'name' => 1, 'v' => 1, 'ns' => 1, 'key' => 1, 'writeConcern' => 1];
-        $indexOptions = array_diff_key($options, $skippedOptions);
         $indexes = iterator_to_array($this->collection->listIndexes());
         $indexCount = count($indexes);
         $collectionExists = true;
+        $indexExists = false;
 
         // listIndexes returns 0 for non-existing collections while the legacy driver returns 1
         if ($indexCount === 0) {
@@ -586,35 +584,32 @@ class MongoCollection
         }
 
         foreach ($indexes as $index) {
-            if ($index->getKey() === $keys) {
-                if ($indexOptions != array_diff_key($index->__debugInfo(), $skippedOptions)) {
-                    throw new \MongoResultException(sprintf('Index with name: %s already exists with different options', $options['name']));
-                }
-
-                return [
-                    'createdCollectionAutomatically' => ! $collectionExists,
-                    'numIndexesBefore' => $indexCount,
-                    'numIndexesAfter' => $indexCount,
-                    'note' => 'all indexes already exist',
-                    'ok' => 1.0
-                ];
-            } elseif ($index->getName() === $options['name']) {
-                throw new \MongoResultException(sprintf('index with name: %s already exists', $index->getName()));
+            if ($index->getKey() === $keys || $index->getName() === $options['name']) {
+                $indexExists = true;
+                break;
             }
         }
 
         try {
             $this->collection->createIndex($keys, $this->convertWriteConcernOptions($options));
         } catch (\MongoDB\Driver\Exception\Exception $e) {
-            throw ExceptionConverter::toLegacy($e);
+            throw ExceptionConverter::toLegacy($e, 'MongoResultException');
         }
 
-        return [
-            'createdCollectionAutomatically' => ! $collectionExists,
+        $result = [
+            'createdCollectionAutomatically' => !$collectionExists,
             'numIndexesBefore' => $indexCount,
-            'numIndexesAfter' => $indexCount + 1,
-            'ok' => 1.0
+            'numIndexesAfter' => $indexCount,
+            'note' => 'all indexes already exist',
+            'ok' => 1.0,
         ];
+
+        if (! $indexExists) {
+            $result['numIndexesAfter']++;
+            unset($result['note']);
+        }
+
+        return $result;
     }
 
     /**