略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoCollection::deleteIndex

2024-12-24

MongoCollection::deleteIndex

(PECL mongo >=0.9.0)

MongoCollection::deleteIndexDeletes an index from this collection

说明

public MongoCollection::deleteIndex ( string|array $keys ) : array

This method is identical to:

<?php

public function deleteIndexes($keys) {
  
$indexName $this->toIndexString($keys);

  return 
$this->db->command(array(
    
"deleteIndexes" => $this->getName(),
    
"index" => $indexName,
  ));
}

?>

Each index is given a unique name when it is created. This is often generated by the driver based on the index key(s) and order/type, but custom names may also be specified with MongoCollection::createIndex()'s "name" option).

Unfortunately, MongoCollection::deleteIndex() cannot delete custom-named indexes due to a backwards compatibility issue. When a string argument is provided, it is assumed to be the single field name in an ascending index (e.g. the name "x_1" would be used for the argument "x"). If an array or object is provided, an index name is generated just as if that argument was passed to MongoCollection::createIndex().

In order to delete a custom-named index with the PHP driver, the deleteIndexes database command must be used. For instance, an index named "myIndex" could be deleted with the PHP driver by running:

<?php

$db
->command(array(
  
"deleteIndexes" => $collection->getName(),
  
"index" => "myIndex",
));

?>

To determine the name of an index with the PHP driver, you can query the system.indexes collection of a database and look for the "name" field of each result. The "ns" field will indicate the collection to which each index belongs.

参数

keys

An array specifying the index's fields as its keys. For each field, the value is either the index direction or » index type. If specifying direction, specify 1 for ascending or -1 for descending.

If a string is provided, it is assumed to be the single field name in an ascending index.

返回值

Returns the database response.

范例

Example #1 MongoCollection::deleteIndex() example

This example passes the function string and array parameters.

<?php

$m 
= new MongoClient();
$c $m->example->indices;

// create and remove a simple index
$c->createIndex(array("i"=>1));
$c->deleteIndex("i");

// create and remove a multi-key index
$c->ensureIndex(array("j" => 1"k" => 1));
$c->deleteIndex(array("j" => 1"k" => 1));

?>

参见

add a note add a note

User Contributed Notes 1 note

up
-2
nanhe dot kumar at gmail dot com
5 years ago
<?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
)

官方地址:https://www.php.net/manual/en/mongocollection.deleteindex.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3