diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 144675b39..d53336fb4 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -216,6 +216,25 @@ public function addColumn($type, $name, array $parameters = []) 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 fd9650dac..ed96e81ff 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -176,6 +176,17 @@ public function testDummies() }); } + 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);