diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 1208ed193..cc62ec6c1 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -234,16 +234,18 @@ public function expire($columns, $seconds) } /** - * @inheritdoc + * Indicate that the collection needs to be created. + * @param array $options + * @return void */ - public function create() + public function create($options = []) { $collection = $this->collection->getCollectionName(); $db = $this->connection->getMongoDB(); // Ensure the collection is created. - $db->createCollection($collection); + $db->createCollection($collection, $options); } /** diff --git a/src/Jenssegers/Mongodb/Schema/Builder.php b/src/Jenssegers/Mongodb/Schema/Builder.php index 1bca6c02e..c01d12426 100644 --- a/src/Jenssegers/Mongodb/Schema/Builder.php +++ b/src/Jenssegers/Mongodb/Schema/Builder.php @@ -33,20 +33,20 @@ public function hasColumns($table, array $columns) /** * Determine if the given collection exists. - * @param string $collection + * @param string $name * @return bool */ - public function hasCollection($collection) + public function hasCollection($name) { $db = $this->connection->getMongoDB(); - foreach ($db->listCollections() as $collectionFromMongo) { - if ($collectionFromMongo->getName() == $collection) { - return true; - } - } + $collections = iterator_to_array($db->listCollections([ + 'filter' => [ + 'name' => $name, + ], + ]), false); - return false; + return count($collections) ? true : false; } /** @@ -134,6 +134,24 @@ protected function createBlueprint($collection, Closure $callback = null) return new Blueprint($this->connection, $collection); } + /** + * Get collection. + * @param string $name + * @return bool|\MongoDB\Model\CollectionInfo + */ + public function getCollection($name) + { + $db = $this->connection->getMongoDB(); + + $collections = iterator_to_array($db->listCollections([ + 'filter' => [ + 'name' => $name, + ], + ]), false); + + return count($collections) ? current($collections) : false; + } + /** * Get all of the collections names for the database. * @return array diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index aa5685df1..575b01113 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -34,6 +34,10 @@ public function testCreateWithOptions(): void Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]); $this->assertTrue(Schema::hasCollection('newcollection_two')); $this->assertTrue(Schema::hasTable('newcollection_two')); + + $collection = Schema::getCollection('newcollection_two'); + $this->assertTrue($collection['options']['capped']); + $this->assertEquals(1024, $collection['options']['size']); } public function testDrop(): void