java 建立索引、刪除索引(background)
阿新 • • 發佈:2019-02-04
public class GGMongoIndexs {
public static String createIndex(MongoCollection<Document> collection, Document indexes){
return collection.createIndex(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;
}
}
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;
}
}