ensureUniqueIndex() public method

Create unique index
public ensureUniqueIndex ( array $key, boolean $dropDups = false ) : Collection
$key array
$dropDups boolean
return Collection
Example #1
0
 public function testEnsureUniqueIndex()
 {
     $this->collection->ensureUniqueIndex(array('uniqueAsc' => 1, 'uniqueDesc' => -1), true);
     $indexes = $this->collection->getIndexes();
     /*
        v.2.6.8
        array (
            0 => array (
                'v' => 1,
                'key' => array (
                    '_id' => 1,
                ),
                'name' => '_id_',
                'ns' => 'test.phpmongo_test_collection',
            ),
            1 => array (
                'v' => 1,
                'unique' => true,
                'key' => array (
                    'uniqueAsc' => 1,
                    'uniqueDesc' => -1,
                ),
                'name' => 'uniqueAsc_1_uniqueDesc_-1',
                'ns' => 'test.phpmongo_test_collection',
                'dropDups' => true,
            ),
        )
     
        v.3.0.0
        array(
            0 => array(
                'v' => 1,
                'key' => array (
                    '_id' => 1,
                ),
                'name' => '_id_',
                'ns' => 'test.phpmongo_test_collection',
            ),
            1 => array (
                'v' => 1,
                'unique' => true,
                'key' => array (
                    'uniqueAsc' => 1,
                    'uniqueDesc' => -1,
                ),
                'name' => 'uniqueAsc_1_uniqueDesc_-1',
                'ns' => 'test.phpmongo_test_collection',
            ),
        )
     */
     $this->assertEquals(array('uniqueAsc' => 1, 'uniqueDesc' => -1), $indexes[1]['key']);
     $currentVersion = $this->collection->getDatabase()->getClient()->getDbVersion();
     if (version_compare($currentVersion, '3', '<')) {
         $this->assertArrayHasKey('dropDups', $indexes[1]);
         $this->assertEquals(1, $indexes[1]['dropDups']);
     }
 }