How to drop an index in MongoDB using PHP?

Member

by lily , in category: PHP Databases , 2 years ago

How to drop an index in MongoDB using PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , 2 years ago

@lily 

To drop an index in MongoDB using PHP, you can use the dropIndex method of the MongoCollection class. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

// connect to the MongoDB server
$mongo = new MongoClient();

// select a database and collection
$db = $mongo->mydb;
$collection = $db->mycollection;

// drop an index
$result = $collection->dropIndex('my_index');

// print the result
var_dump($result);

?>


In this example, we connect to the MongoDB server, select a database and collection, and call the dropIndex method on the collection, passing in the name of the index we want to drop. The method returns a boolean value indicating whether the index was successfully dropped or not.