Rename Collection in MongoDB using Java
In this example, We will show you sample program about, How to rename collection in mongodb using Java. The same program has been attached in the below post.
Example Program
package com.dineshkrish.mongo; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoNamespace; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; /** * * @author Dinesh Krishnan * */ public class DeleteDocument { public static void main(String[] args) { // host name final String HOST_NAME = "localhost"; // default port number final int PORT = 27017; // creating mongo client object MongoClient client = new MongoClient(HOST_NAME, PORT); // selecting the mongo database MongoDatabase database = client.getDatabase("dineshkrish"); // selecting the mongo collection MongoCollection<Document> collection = database .getCollection("employee_records"); // creating namespace MongoNamespace newName = new MongoNamespace("dineshkrish" ,"new_employee_records"); // renaming the collection collection.renameCollection(newName); System.out.println("Collection has been renamed"); // closing the client client.close(); } }
References
1. MongoDB Java API Documentation
2. JavaDoc – MongoClient Class
3. JavaDoc – MongoDatabase Interface
4. JavaDoc – Document Class
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet