List of usage examples for com.mongodb.client MongoCollection renameCollection
void renameCollection(MongoNamespace newCollectionNamespace);
From source file:com.imaginea.mongodb.services.impl.CollectionServiceImpl.java
License:Apache License
/** * Creates a collection inside a database in mongo to which user is connected to. * * @param dbName Name of Database in which to insert a collection * @param selectedCollectionName Collection on which the operation is performed * @param newCollName Name of Collection to be added/renamed to * @param capped Specify if the collection is capped * @param size Specify the size of collection * @param maxDocs specify maximum no of documents in the collection * @return Success if Insertion is successful else throw exception * @throws DatabaseException throw super type of UndefinedDatabaseException * @throws ValidationException throw super type of * EmptyDatabaseNameException,EmptyCollectionNameException * @throws CollectionException throw super type of * DuplicateCollectionException,InsertCollectionException *//*from ww w . j a v a2 s . c o m*/ public String updateCollection(String dbName, String selectedCollectionName, String newCollName, boolean capped, long size, int maxDocs, boolean autoIndexId) throws DatabaseException, CollectionException, ValidationException { if (dbName == null || dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Invalid Database name"); } if (selectedCollectionName == null || newCollName == null) { throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Collection name should be provided"); } if (selectedCollectionName.equals("") || newCollName.equals("")) { throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Collection name cannot be empty"); } String result = "No updates were specified!"; try { // if (!databaseService.getDbList().contains(dbName)) { // throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS, // "Db with name [" + dbName + "] doesn't exist."); // } boolean convertedToCapped = false, convertedToNormal = false, renamed = false; MongoDatabase db = mongoInstance.getDatabase(dbName); MongoCollection<Document> selectedCollection = db.getCollection(selectedCollectionName); CreateCollectionOptions options = new CreateCollectionOptions(); options.capped(capped); if (capped) { options.maxDocuments(maxDocs); options.autoIndex(autoIndexId); options.sizeInBytes(size); createCollection(options, selectedCollection, selectedCollectionName, db); convertedToCapped = true; } else { createCollection(options, selectedCollection, selectedCollectionName, db); convertedToNormal = true; } if (!selectedCollectionName.equals(newCollName)) { if (getCollList(dbName).contains(newCollName)) { throw new CollectionException(ErrorCodes.COLLECTION_ALREADY_EXISTS, "Collection [" + newCollName + "] already exists in Database [" + dbName + "]"); } selectedCollection = db.getCollection(selectedCollectionName); MongoNamespace mongoNamespace = new MongoNamespace(dbName + "." + newCollName); selectedCollection.renameCollection(mongoNamespace); renamed = true; } if ((convertedToNormal || convertedToCapped) && renamed) { result = "Collection [" + selectedCollectionName + "] was successfully updated."; } else if (convertedToCapped) { result = "Collection [" + selectedCollectionName + "] was successfully converted to capped collection"; } else if (convertedToNormal) { result = "Capped Collection [" + selectedCollectionName + "] was successfully converted to normal collection"; } else if (renamed) { result = "Collection [" + selectedCollectionName + "] was successfully renamed to '" + newCollName + "'"; } } catch (MongoException m) { throw new CollectionException(ErrorCodes.COLLECTION_UPDATE_EXCEPTION, m.getMessage()); } return result; }
From source file:com.imaginea.mongodb.services.impl.CollectionServiceImpl.java
License:Apache License
/** * Deletes a collection inside a database in mongo to which user is connected to. * * @param dbName Name of Database in which to insert a collection * @param collectionName Name of Collection to be inserted * @return Success if deletion is successful else throw exception * @throws DatabaseException throw super type of UndefinedDatabaseException * @throws ValidationException throw super type of * EmptyDatabaseNameException,EmptyCollectionNameException * @throws CollectionException throw super type of * UndefinedCollectionException,DeleteCollectionException *//*from ww w . jav a 2 s. c o m*/ private void createCollection(CreateCollectionOptions options, MongoCollection<Document> selectedCollection, String selectedCollectionName, MongoDatabase db) { db.createCollection(selectedCollectionName + "_temp", options); MongoCollection<Document> tempCollection = db.getCollection(selectedCollectionName + "_temp"); MongoCursor<Document> cur = selectedCollection.find().iterator(); while (cur.hasNext()) { Document obj = cur.next(); tempCollection.insertOne(obj); } MongoNamespace namespace = selectedCollection.getNamespace(); selectedCollection.drop(); tempCollection.renameCollection(namespace); }
From source file:mongofx.ui.dbtree.DBTreeController.java
License:Open Source License
private void onReanameCollection(ActionEvent actionEvent) { TreeItem<DbTreeValue> selectedItem = treeView.getSelectionModel().getSelectedItem(); if (selectedItem == null) { return;//from ww w .ja v a 2 s .c o m } DbTreeValue value = selectedItem.getValue(); TextInputDialog dialog = new TextInputDialog(value.getDisplayValue()); dialog.setContentText("New collection name:"); dialog.setHeaderText("Rename collection"); dialog.showAndWait().ifPresent(targetCollection -> { MongoCollection<Document> collection = value.getMongoDatabase().getMongoDb() .getCollection(value.getDisplayValue()); collection.renameCollection(new MongoNamespace(value.getMongoDatabase().getName(), targetCollection)); ((DynamicTreeItem) selectedItem.getParent()).reload(); }); }