
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- How to create an index in MongoDB using Java?
- How to drop a database in MongoDB using java?
- How to drop a MongoDB Collection 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 update an existing document in MongoDB collection using Java?
- How to remove an array element by its index in MongoDB?
- Drop Collection if already exists in MongoDB using Python
- How to index and sort with pagination using custom field in MongoDB?
- How to drop a numeric collection from MongoDB?
- How to automate drag & drop functionality using Selenium WebDriver Java?
- How to create a nested index in MongoDB?
- How to create a database in MongoDB using Java?

Advertisements