w3resource

MongoDB: db.collection. dropIndexes() method

db.collection.dropIndexes

The db.collection.dropIndexes() method is used to drop all indexes other than the required index on the _id field. Only call dropIndexes() as a method on a collection object.

Syntax:

db.collection.dropIndexes()

Example: MongoDB: db.collection.dropIndexes() method

Here we have create a duplicate of restaurants collection restaurants1 and the following indexes we have created on restaurants1 collection.

> db.restaurants1.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.restaurants1"
        },
        {
                "v" : 1,
                "key" : {
                        "cuisine" : 1
                },
                "name" : "cuisine_1",
                "ns" : "test.restaurants1"
        },
        {
                "v" : 1,
                "key" : {
                        "cuisine" : 1,
                        "address.zipcode" : -1
                },
                "name" : "cuisine_1_address.zipcode_-1",
                "ns" : "test.restaurants1"
        }
]

The single field index on the fieldcuisinehas the user-specified name ofcuisine_1and the index specification document of{"cuisine":1} and a compound index on thecuisinefield (in ascending order) and the zipcodefield (in descending order.).

Now, the following statement will drop all the indexes from restaurants1 collection.

db.restaurants1.dropIndexes();

Here is the output

{
        "nIndexesWas" : 2,
        "msg" : "non-_id indexes dropped for collection",
        "ok" : 1
}

Now, lists the indexes again.

> db.restaurants1.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.restaurants1"
        }
]

Retrieve the restaurants data from here

Previous: db.collection.dropIndex() method
Next: db.collection.explain() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/shell-methods/collection/db-collection-dropIndexes.php