Sfoglia il codice sorgente

Merge pull request #195 from robwil/updatedexisting

improve updatedExisting calculation for update result
Andreas 8 anni fa
parent
commit
9fdb55f6c4

+ 5 - 4
lib/Mongo/MongoCollection.php

@@ -371,8 +371,9 @@ class MongoCollection
      * @param array $criteria Description of the objects to update.
      * @param array $newobj The object with which to update the matching records.
      * @param array $options
-     * @throws MongoCursorException
-     * @return boolean
+     * @return bool|array
+     * @throws MongoException
+     * @throws MongoWriteConcernException
      */
     public function update(array $criteria, array $newobj, array $options = [])
     {
@@ -410,7 +411,7 @@ class MongoCollection
             'n' => $result->getMatchedCount(),
             'err' => null,
             'errmsg' => null,
-            'updatedExisting' => $result->getUpsertedCount() == 0,
+            'updatedExisting' => $result->getUpsertedCount() == 0 && $result->getModifiedCount() > 0,
         ];
     }
 
@@ -813,7 +814,7 @@ class MongoCollection
                 'n' => $result->getUpsertedCount() + $result->getModifiedCount(),
                 'err' => null,
                 'errmsg' => null,
-                'updatedExisting' => $result->getUpsertedCount() == 0,
+                'updatedExisting' => $result->getUpsertedCount() == 0 && $result->getModifiedCount() > 0,
             ];
             if ($result->getUpsertedId() !== null) {
                 $resultArray['upserted'] = TypeConverter::toLegacy($result->getUpsertedId());

+ 20 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -392,6 +392,26 @@ class MongoCollectionTest extends TestCase
         $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
     }
 
+    public function testUpdateWhichDoesntMatchQuery()
+    {
+        $document = ['foo' => 'bar'];
+        $this->getCollection()->insert($document);
+
+        $expected = [
+            'ok' => 1.0,
+            'nModified' => 0,
+            'n' => 0,
+            'err' => null,
+            'errmsg' => null,
+            'updatedExisting' => false,
+        ];
+
+        $result = $this->getCollection()->update(['foo' => 'bar22'], ['$set' => ['foo' => 'foo']]);
+        $this->assertEquals($expected, $result);
+
+        $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'bar']));
+    }
+
     public function testUnacknowledgedUpdate()
     {
         $document = ['foo' => 'bar'];