একটি সূচক ড্রপ করার জন্য MongoDB-তে, আপনাকে dropIndex() ব্যবহার করতে হবে পদ্ধতি।
সিনট্যাক্স
db.COLLECTION_NAME.dropIndex({KEY:1}) জাভাতে, আপনি dropIndex() ব্যবহার করে একটি সূচক ড্রপ করতে পারেন পদ্ধতি, এই পদ্ধতিতে আপনাকে সূচকের ধরন (উড়োহী বা অবরোহ) এবং যে ক্ষেত্রে আপনি এটি তৈরি করেছেন তার নাম পাস করতে হবে।
dropIndex(Indexes.ascending("name")); উদাহরণ
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());
}
}
} আউটপুট
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"}