

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to drop an index in MongoDB using Java?
In MongoDB to drop an index, you need to use dropIndex() method.
Syntax
db.COLLECTION_NAME.dropIndex({KEY:1})
In Java, you can drop an Index using the dropIndex() method, to this method you need to pass the type of the index (ascending or descending) and the field name on which you have created it.
dropIndex(Indexes.ascending("name"));
Example
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Indexes; import org.bson.Document; import com.mongodb.MongoClient; public class DroppingIndex { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Accessing the database MongoDatabase database = mongo.getDatabase("myDatabase"); //Creating a collection database.createCollection("sampleCollection"); //Retrieving the collection on which you want to create the index MongoCollection coll = database.getCollection("sampleCollection"); //Creating indexes coll.createIndex(Indexes.ascending("age")); coll.createIndex(Indexes.ascending("name")); System.out.println("List of colections: "); for (Document index : coll.listIndexes()) { System.out.println(index.toJson()); } //Dropping the index coll = database.getCollection("sampleCollection"); coll.dropIndex(Indexes.ascending("name")); System.out.println("List of colections after deleting one "); for (Document index : coll.listIndexes()) { System.out.println(index.toJson()); } } }
Output
List of colections: {"v": 2, "key": {"_id": 1}, "name": "_id_", "ns": "myDatabase.sampleCollection"} {"v": 2, "key": {"age": 1}, "name": "age_1", "ns": "myDatabase.sampleCollection"} {"v": 2, "key": {"name": 1}, "name": "name_1", "ns": "myDatabase.sampleCollection"} List of colections after deleting one {"v": 2, "key": {"_id": 1}, "name": "_id_", "ns": "myDatabase.sampleCollection"} {"v": 2, "key": {"age": 1}, "name": "age_1", "ns": "myDatabase.sampleCollection"}
- Related Questions & Answers
- How to create an index in MongoDB using Java?
- How to drop a MongoDB Collection using Java?
- How to drop a database in MongoDB using java?
- How to create an index with MongoDB?
- How to Drop MySQL Table using Java?
- How to drop a database in MongoDB?
- How to drop a collection in MongoDB?
- How to drop a numeric collection from MongoDB?
- How to remove an array element by its index in MongoDB?
- How to update an existing document in MongoDB collection using Java?
- Create an index for text search in MongoDB
- How to automate drag & drop functionality using Selenium WebDriver Java?
- How to force MongoDB to use the BasicCursor instead of an index?
- How can we drop an existing database by using mysqladmin?
- How to index and sort with pagination using custom field in MongoDB?
Advertisements