public function testDropAllIndexesByWildcard() { $indexes = [['key' => ['x' => 1]], ['key' => ['y' => 1]]]; $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes); $createdIndexNames = $operation->execute($this->getPrimaryServer()); $this->assertSame('x_1', $createdIndexNames[0]); $this->assertSame('y_1', $createdIndexNames[1]); $this->assertIndexExists('x_1'); $this->assertIndexExists('y_1'); $operation = new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), '*'); $this->assertCommandSucceeded($operation->execute($this->getPrimaryServer())); $operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName()); $indexes = $operation->execute($this->getPrimaryServer()); foreach ($indexes as $index) { if ($index->getName() === 'x_1') { $this->fail('The "x_1" index should have been deleted'); } if ($index->getName() === 'y_1') { $this->fail('The "y_1" index should have been deleted'); } } }
/** * Create one or more indexes for the collection. * * Each element in the $indexes array must have a "key" document, which * contains fields mapped to an order or type. Other options may follow. * For example: * * $indexes = [ * // Create a unique index on the "username" field * [ 'key' => [ 'username' => 1 ], 'unique' => true ], * // Create a 2dsphere index on the "loc" field with a custom name * [ 'key' => [ 'loc' => '2dsphere' ], 'name' => 'geo' ], * ]; * * If the "name" option is unspecified, a name will be generated from the * "key" document. * * @see http://docs.mongodb.org/manual/reference/command/createIndexes/ * @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/ * @param array[] $indexes List of index specifications * @return string[] The names of the created indexes * @throws InvalidArgumentException if an index specification is invalid */ public function createIndexes(array $indexes) { $operation = new CreateIndexes($this->databaseName, $this->collectionName, $indexes); $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); return $operation->execute($server); }
public function testCreateConflictingIndexesWithLegacyInsert() { if (\MongoDB\server_supports_feature($this->getPrimaryServer(), self::$wireVersionForCommand)) { $this->markTestSkipped('Index creation does not use legacy insertion'); } $indexes = [['key' => ['x' => 1], 'sparse' => true, 'unique' => false], ['key' => ['x' => 1], 'sparse' => false, 'unique' => true]]; $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes); $createdIndexNames = $operation->execute($this->getPrimaryServer()); /* When creating indexes with legacy insert operations, the server * ignores conflicting index specifications and leaves the original * index in place. */ $this->assertSame('x_1', $createdIndexNames[0]); $this->assertIndexExists('x_1', function (IndexInfo $info) { $this->assertTrue($info->isSparse()); $this->assertFalse($info->isUnique()); $this->assertFalse($info->isTtl()); }); }