1. 程式人生 > >java 建立索引、刪除索引(background)

java 建立索引、刪除索引(background)

public class GGMongoIndexs {


public static String createIndex(MongoCollection<Document> collection, Document indexes){
return collection.createIndex(indexes);

}

       //後臺建立索引

public static String createIndexBackGround(MongoCollection<Document> collection, Document indexes){
IndexOptions options = new IndexOptions();
options.background(true);
return collection.createIndex(indexes, options);
}
public static List<String> createIndex(MongoCollection<Document> collection, List<IndexModel> indexes){
return collection.createIndexes(indexes);
}
public static void dropIndex(MongoCollection<Document> collection, Document indexes){
collection.dropIndex(indexes);
}
public static void dropIndexAll(MongoCollection<Document> collection){
collection.dropIndexes();
}
public static List<JSONObject> listIndexs(MongoCollection<Document> collection){

ListIndexesIterable<Document> list = collection.listIndexes();
MongoCursor<Document> cursor = list.iterator();
List<JSONObject> indexs = new ArrayList<JSONObject>();
while(cursor.hasNext()){
Document o = cursor.next();
JSONObject json = JSONObject.fromObject(o);
indexs.add(json.getJSONObject("key"));
}
cursor.close();

return indexs;
}
}