diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 5d2edbce815bf0e007a13ee2967129714e795cab..0d0e1cb8fe08a4def663ab75764db36e1fb96612 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -222,6 +222,25 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint return $this; } + /** + * Specify a sparse and unique index for the collection. + * + * @param string|array $columns + * @param array $options + * @return Blueprint + */ + public function sparse_and_unique($columns = null, $options = []) + { + $columns = $this->fluent($columns); + + $options['sparse'] = true; + $options['unique'] = true; + + $this->index($columns, $options); + + return $this; + } + /** * Allow fluent columns. * diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 1a1c3a96c60b70cbdb7e2e60872f470a09264669..6f7b0f0e23e3abd7f3ca880bcd7f77f7fb334297 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -175,6 +175,17 @@ class SchemaTest extends TestCase }); } + public function testSparseUnique() + { + Schema::collection('newcollection', function ($collection) { + $collection->sparse_and_unique('sparseuniquekey'); + }); + + $index = $this->getIndex('newcollection', 'sparseuniquekey'); + $this->assertEquals(1, $index['sparse']); + $this->assertEquals(1, $index['unique']); + } + protected function getIndex($collection, $name) { $collection = DB::getCollection($collection);