How to Rename Collection in MongoDB using Java

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
		// creating namespace
		MongoNamespace newName = new MongoNamespace("dineshkrish" ,"new_employee_records");
		// renaming the collection
		System.out.println("Collection has been renamed");
		// closing the client



1. MongoDB Java API Documentation
2. JavaDoc – MongoClient Class
3. JavaDoc – MongoDatabase Interface
4. JavaDoc – Document Class