摘要:Java MongoDB 建立索引
public boolean createInboxIndex(String index_key) {
boolean scuess = true;
try {
DBCollection collection = db.getCollection(index_key);
BasicDBObject indexDbo = new BasicDBObject();
indexDbo.append("key_name1", 1);
indexDbo.append("key_name2", 1);
collection.ensureIndex(indexDbo, index_key, true);
collection = null;
indexDbo = null;
} catch (Exception ex) {
scuess = false;
}
return scuess;
}
public boolean hasIndex(String index_key) {
boolean flag = false;
try {
DBCollection collection = db.getCollection("system.indexes");
BasicDBObject indexDbo = new BasicDBObject();
indexDbo.append("name", index_key);
DBCursor cursor = collection.find(indexDbo);
if (cursor.hasNext()) {
flag = true;
}
cursor.close();
cursor = null;
collection = null;
indexDbo = null;
} catch (Exception ex) {
flag = false;
}
return flag;
}