MongoCollection::deleteIndex
(PECL mongo >=0.9.0)
MongoCollection::deleteIndex — Deletes an index from this collection
Описание
$keys
)This method is identical to:
<?php
public function deleteIndexes($keys) {
// toIndexString is a protected method that turns strings, arrays, and objs
//into index names
$index = $this->toIndexString($keys);
return $this->db->command(array("deleteIndexes" => $this->getName(),
"index" => $index));
}
?>
Each index, when created, is given a unique name. This is generally user-set (with MongoCollection::ensureIndex()'s "name" option) or generated by the driver from a combination of key names and directions. This name is then used by MongoCollection::deleteIndex() to remove the function.
Unfortunately, the MongoCollection::ensureIndex() generates slightly different names than the shell and, due to backwards compatibility issues, MongoCollection::deleteIndex() cannot delete custom-named indexes as well. Thus, the best way to delete indexes created in the shell or with custom names is to directly call the deleteIndexes database command.
Thus, if you named an index "superfast query", you could only delete it with the PHP driver by running:
<?php
$db->command(array("deleteIndexes" => $collection->getName(), "index" => "superfast query"));
?>
To find what an index is named, you can query the system.indexes collection of a database and look for the name field.
Список параметров
-
keys
-
Field or fields from which to delete the index.
Возвращаемые значения
Returns the database response.
Примеры
Пример #1 MongoCollection::deleteIndex() example
This example passes the function string and array parameters.
<?php
$m = new MongoClient();
$c = $m->example->indices;
// create an index
$c->ensureIndex(array("i"=>1));
// remove a simple index
$c->deleteIndex("i");
// create a multi-key index
$c->ensureIndex(array("j" => 1, "k" => 1));
// remove a multi-key index
$c->deleteIndex(array("j" => 1, "k" => 1));
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MongoDB
- Базовые классы
- Функция MongoCollection::aggregate() - Perform an aggregation using the aggregation framework
- Функция MongoCollection::aggregateCursor() - Execute an aggregation pipeline command and retrieve results through a cursor
- Функция MongoCollection::batchInsert() - Inserts multiple documents into this collection
- Функция MongoCollection::__construct() - Creates a new collection
- Функция MongoCollection::count() - Counts the number of documents in this collection
- Функция MongoCollection::createDBRef() - Creates a database reference
- Функция MongoCollection::createIndex() - Creates an index on the specified field(s) if it does not already exist.
- Функция MongoCollection::deleteIndex() - Deletes an index from this collection
- Функция MongoCollection::deleteIndexes() - Delete all indices for this collection
- Функция MongoCollection::distinct() - Retrieve a list of distinct values for the given key across a collection.
- Функция MongoCollection::drop() - Drops this collection
- Функция MongoCollection::ensureIndex() - Creates an index on the specified field(s) if it does not already exist.
- MongoCollection::find
- Функция MongoCollection::findAndModify() - Update a document and return it
- Функция MongoCollection::findOne() - Queries this collection, returning a single element
- Функция MongoCollection::__get() - Gets a collection
- Функция MongoCollection::getDBRef() - Fetches the document pointed to by a database reference
- Функция MongoCollection::getIndexInfo() - Returns information about indexes on this collection
- Функция MongoCollection::getName() - Returns this collection's name
- Функция MongoCollection::getReadPreference() - Get the read preference for this collection
- Функция MongoCollection::getSlaveOkay() - Get slaveOkay setting for this collection
- Функция MongoCollection::getWriteConcern() - Get the write concern for this collection
- Функция MongoCollection::group() - Performs an operation similar to SQL's GROUP BY command
- Функция MongoCollection::insert() - Inserts a document into the collection
- Функция MongoCollection::parallelCollectionScan() - Returns an array of cursors to iterator over a full collection in parallel
- Функция MongoCollection::remove() - Remove records from this collection
- Функция MongoCollection::save() - Saves a document to this collection
- Функция MongoCollection::setReadPreference() - Set the read preference for this collection
- Функция MongoCollection::setSlaveOkay() - Change slaveOkay setting for this collection
- Функция MongoCollection::setWriteConcern() - Set the write concern for this database
- Функция MongoCollection::toIndexString() - Converts keys specifying an index to its identifying string
- Функция MongoCollection::__toString() - String representation of this collection
- Функция MongoCollection::update() - Update records based on a given criteria
- Функция MongoCollection::validate() - Validates this collection
Коментарии
<?php
/*
How can delete Index through Index name.
*/
function deleteIndex($db, $collection, $indexName) {
if (class_exists("MongoClient")) {
$m = new MongoClient();
} else {
$m = new Mongo();
}
$indexes = $m->{$db}->{$collection}->getIndexInfo();
foreach ($indexes as $index) {
if ($index['name'] === $indexName) {
return $m->{$db}->command(array("deleteIndexes" => $this->m->{$db}->{$collection}->getName(), "index" =>$index['key']));
break;
}
}
return false;
}
$response=deleteIndex('student','class','roll');
echo "<pre>;
print_r($response);
echo "</pre>";
?>
Array
(
[nIndexesWas] => 2
[ok] => 1
)