Commit 2dcfa5b5 authored by Ditty's avatar Ditty

Add getCollection function and little improvements

Add getCollection function and little improvements
parent bf1e25ae
......@@ -33,21 +33,20 @@ class Builder extends \Illuminate\Database\Schema\Builder
/**
* Determine if the given collection exists.
* @param string $collection
* @param bool $collection_info
* @return bool|\MongoDB\Model\CollectionInfo
* @param string $name
* @return bool
*/
public function hasCollection($collection, $collection_info = false)
public function hasCollection($name)
{
$db = $this->connection->getMongoDB();
foreach ($db->listCollections() as $collectionFromMongo) {
if ($collectionFromMongo->getName() == $collection) {
return $collection_info ? $collectionFromMongo : true;
}
}
$collections = $db->listCollections([
'filter' => [
'name' => $name,
],
]);
return false;
return $collections->count() ? true : false;
}
/**
......@@ -135,6 +134,24 @@ class Builder extends \Illuminate\Database\Schema\Builder
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 = $db->listCollections([
'filter' => [
'name' => $name,
],
]);
return $collections->count() ? (($collections = iterator_to_array($collections) ) ? current($collections) : false) : false;
}
/**
* Get all of the collections names for the database.
* @return array
......
......@@ -35,7 +35,7 @@ class SchemaTest extends TestCase
$this->assertTrue(Schema::hasCollection('newcollection_two'));
$this->assertTrue(Schema::hasTable('newcollection_two'));
$collection = Schema::hasCollection('newcollection_two', true);
$collection = Schema::getCollection('newcollection_two');
$this->assertTrue($collection['options']['capped']);
$this->assertEquals(1024, $collection['options']['size']);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment