MongoDb集合改名
public boolean mongoDbCollectionNameUpdate(String oldName,String newName){
/*连接MongoDB数据库*/
MongoClient mongoClient = MongoClients.create(mongourl);
MongoDatabase country = mongoClient.getDatabase("Country");
/*判断是否存在该集合*/
boolean collectionExists = country.listCollectionNames().into(new ArrayList<String>()).contains(oldName);
/*改名*/
if (collectionExists){
MongoCollection<Document> collection = country.getCollection(oldName);
collection.renameCollection(new MongoNamespace("Country",newName),new RenameCollectionOptions().dropTarget(false));
return true;
}
System.out.println("该集合不存在");
return false;
}
posted on 2023-03-14 20:30 brickMovingWorker 阅读(19) 评论(0) 编辑 收藏 举报