update() публичный Метод

public update ( $collectionName, $filter, $update, array $options = [] )
$options array
Пример #1
0
 /**
  * Save all the entries from the buffer.
  */
 public function save()
 {
     $entries = $this->logBuffer->getEntries();
     $entrySkeleton = ['ts' => $this->ts, 'month' => $this->month, 'year' => $this->year];
     $dimensionSkeleton = ['ts' => $this->ts, 'month' => $this->month, 'year' => $this->year];
     /**
      * @var $e LogEntry
      */
     foreach ($entries as $e) {
         // build entry
         $entry = $entrySkeleton;
         $entry['entity'] = $e->getName();
         $entry['ref'] = $e->getRef();
         // insert or update the DAILY stat
         $this->mongo->update(self::ADB_STATS_DAILY, ['entity' => $e->getName(), 'ref' => $e->getRef(), 'ts' => $this->ts], ['$inc' => ['count' => $e->getIncrement()], '$setOnInsert' => $entry], ['upsert' => true]);
         // insert or update the MONTHLY stat
         unset($entry['ts']);
         $entry['month'] = $this->month;
         $entry['year'] = $this->year;
         $this->mongo->update(self::ADB_STATS_MONTHLY, ['entity' => $e->getName(), 'ref' => $e->getRef(), 'ts' => $this->monthTs], ['$inc' => ['count' => $e->getIncrement()], '$setOnInsert' => $entry], ['upsert' => true]);
         // insert dimensions for the entry
         $dimEntry = $dimensionSkeleton;
         $dimEntry['entity'] = $e->getName();
         /**
          * @var $dim LogDimension
          */
         foreach ($e->getDimensions() as $dim) {
             $dimEntry['name'] = $dim->getName();
             $dimEntry['value'] = $dim->getValue();
             $this->mongo->update(self::ADB_DIMS, ['name' => $dim->getName(), 'value' => $dim->getValue(), 'entity' => $e->getName(), 'ts' => $this->ts], ['$inc' => ['count.' . $e->getRef() => $dim->getIncrement(), 'total' => $dim->getIncrement()], '$setOnInsert' => $dimEntry], ['upsert' => true]);
         }
     }
     // clear buffer
     $this->logBuffer = new LogBuffer();
 }
Пример #2
0
 /**
  * @dataProvider driverSet
  */
 public function testMongo(Mongo $mongo)
 {
     $collection = 'TestCollection';
     $mongo->dropCollection($collection);
     // Test create collection
     $mongo->createCollection($collection);
     $collections = $mongo->listCollections();
     /* @var $c CollectionInfo */
     $collectionNames = [];
     foreach ($collections as $c) {
         $collectionNames[] = $c->getName();
     }
     $this->assertContains('Mongo_' . $collection, $collectionNames);
     // Test insert data
     $data = ['name' => 'Webiny'];
     $result = $mongo->insertOne($collection, $data);
     $this->assertEquals(1, $result->getInsertedCount());
     $this->assertEquals(1, $mongo->count($collection));
     // Get new record ID
     $id = $result->getInsertedId();
     // Test update
     $res = $mongo->update($collection, ['_id' => $id], ['$set' => ['name' => 'Updated Webiny']]);
     $this->assertEquals(1, $res->getModifiedCount());
     // Test findOne
     $data = $mongo->findOne($collection, ['_id' => $id]);
     $this->assertEquals('Updated Webiny', $data['name']);
     // Test find
     $data = $mongo->find($collection, ['name' => 'Updated Webiny']);
     $this->assertCount(1, $data);
     // Test insertMany
     $mongo->insertMany($collection, [['name' => 'Webiny', 'tag' => 1], ['name' => 'Webiny', 'tag' => 2], ['name' => 'Webiny', 'tag' => 3], ['name' => 'Webiny', 'tag' => 4], ['name' => 'Webiny', 'tag' => 5]]);
     $this->assertEquals(6, $mongo->count($collection));
     // Test find with offset
     $results = $mongo->find($collection, ['name' => 'Webiny'], [], 1, 2);
     $this->assertEquals(3, $results[0]['tag']);
     $results = $mongo->find($collection, ['name' => 'Webiny'], ['tag' => -1], 2, 3);
     $this->assertCount(2, $results);
     $this->assertEquals(2, $results[0]['tag']);
     $this->assertEquals(1, $results[1]['tag']);
     // Test remove data
     $mongo->delete($collection, ['_id' => $id]);
     $this->assertEquals(5, $mongo->count($collection));
     // Test drop collection
     $mongo->dropCollection($collection);
     $collections = $mongo->listCollections();
     /* @var $c CollectionInfo */
     $collectionNames = [];
     foreach ($collections as $c) {
         $collectionNames[] = $c->getName();
     }
     $this->assertFalse(in_array($collection, $collectionNames));
     // Test isId()
     $id = '12345678absdfgrtuierwe12';
     $this->assertFalse($mongo->isId($id));
     $id = 'aaaabbbbcccc 11122223333';
     $this->assertFalse($mongo->isId($id));
     $id = '543c1d846803fa76058b458b';
     $this->assertTrue($mongo->isId($id));
 }
Пример #3
0
 /**
  * @dataProvider driverSet
  */
 public function testMongo(Mongo $mongo)
 {
     $collection = 'TestCollection';
     $mongo->dropCollection($collection);
     // Test create collection
     $mongo->createCollection($collection);
     $collectionNames = $mongo->getCollectionNames();
     $this->assertContains($collection, $collectionNames);
     // Test insert data
     $data = ['name' => 'Webiny'];
     $mongo->insert($collection, $data);
     $this->assertEquals(1, $mongo->count($collection));
     $this->assertArrayHasKey('_id', $data);
     // Get new record ID
     $id = $data['_id'];
     // Test update and findOne
     $mongo->update($collection, ['_id' => $id], ['name' => 'Updated Webiny']);
     $data = $mongo->findOne($collection, ['_id' => $id]);
     $this->assertEquals('Updated Webiny', $data['name']);
     // Test find
     $data = $mongo->find($collection, ['name' => 'Updated Webiny']);
     $this->assertCount(1, $data);
     // Test ensureIndex
     $res = $mongo->ensureIndex($collection, 'name');
     $this->assertEquals(1, $res['ok']);
     // Test remove data
     $mongo->remove($collection, ['_id' => $id]);
     $this->assertEquals(0, $mongo->count($collection));
     // Test save
     $data = ['name' => 'Webiny Save'];
     $res = $mongo->save($collection, $data);
     $this->assertEquals(1, $res['ok']);
     // Test drop collection
     $mongo->dropCollection($collection);
     $this->assertFalse(in_array($collection, $mongo->getCollectionNames()->toArray()));
     // Test isMongoId()
     $id = '12345678absdfgrtuierwe12';
     $this->assertFalse($mongo->isMongoId($id));
     $id = 'aaaabbbbcccc 11122223333';
     $this->assertFalse($mongo->isMongoId($id));
     $id = '543c1d846803fa76058b458b';
     $this->assertTrue($mongo->isMongoId($id));
 }
Пример #4
0
 /**
  * Save the given entry into index.
  *
  * @param string $key Cache key.
  * @param array  $tags List of tags attached to the entry.
  * @param int    $ttl Unix timestamp until when the cache entry is considered valid.
  *
  * @return bool True if save was successful, otherwise false.
  */
 public function save($key, array $tags, $ttl)
 {
     $this->mongoInstance->update(self::collection, ['key' => $key], ['$set' => ['key' => $key, 'ttl' => new UTCDatetime((time() + $ttl) * 1000), 'tags' => $tags]], ['upsert' => true]);
     return true;
 }