listCollections() public method

List collections
public listCollections ( array $options = [] ) : mixed
$options array
return mixed
コード例 #1
0
ファイル: MongoTest.php プロジェクト: Webiny/Framework
 /**
  * @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));
 }
コード例 #2
0
ファイル: Mongo.php プロジェクト: webiny/hrc
 /**
  * Installs the required mongo collection for cache storage and creates the required indexes.
  *
  * @return bool
  */
 public function installCollections()
 {
     $collections = $this->mongoInstance->listCollections();
     foreach ($collections as $collection) {
         /* @var $collection CollectionInfo */
         if ($collection->getName() == self::collection) {
             return true;
         }
     }
     $this->mongoInstance->createCollection(self::collection);
     $this->mongoInstance->createIndex(self::collection, new SingleIndex('key', 'key', false, true));
     $this->mongoInstance->createIndex(self::collection, new SingleIndex('ttl', 'ttl', false, false, false, 0));
     return true;
 }
コード例 #3
0
ファイル: AnalyticsDb.php プロジェクト: Webiny/AnalyticsDb
 /**
  * Creates the necessary indexes and collections if they don't exist.
  */
 private function createCollections()
 {
     $collections = $this->mongo->listCollections();
     $collectionsCreated = false;
     foreach ($collections as $collection) {
         /* @var $collection CollectionInfo */
         if ($collection->getName() == self::ADB_STATS_DAILY) {
             $collectionsCreated = true;
             break;
         }
     }
     if (!$collectionsCreated) {
         // create collections
         $this->mongo->createCollection(self::ADB_STATS_DAILY);
         $this->mongo->createCollection(self::ADB_STATS_MONTHLY);
         $this->mongo->createCollection(self::ADB_DIMS);
         // ensure indexes
         $this->mongo->createIndex(self::ADB_STATS_DAILY, new CompoundIndex('entityTsEntry', ['entity', 'ref', 'ts'], true, true));
         $this->mongo->createIndex(self::ADB_STATS_MONTHLY, new CompoundIndex('entityMonthEntry', ['entity', 'ref', 'ts'], true, true));
         $this->mongo->createIndex(self::ADB_DIMS, new CompoundIndex('dimension', ['name', 'value', 'entity', 'ts'], true, true));
         $this->mongo->createIndex(self::ADB_DIMS, new CompoundIndex('dimension_entity', ['entity', 'ts'], true));
     }
 }