How to Rename a Collection in MongoDB with Nodejs?

This simple and easy guide will discuss how to rename a database in MongoDB with Nodejs.

Creating or learning to design an application requires a lot of trial and error. At times, you may be unsure about what features your apps must have. Making a decision may be difficult at times.

Then there are times when you want to entirely change things, get rid of them for good, or even add or change something. After your project and database have been built, you may want to add a new collection, field, or data to a MongoDB database or document.

If you’ve ever looked through the MongoDB documentation, you’ve undoubtedly seen how many operators, functions, and other features there are.

It helps developers to improve the utility, usability, and sophistication of their programs. Even novices and rookies to MongoDB may comprehend these operators, as well as their syntaxes and documentation.

MongoDB’s numerous strong capabilities and functions are unparalleled. Modern NoSQL databases, such as MongoDB, provide excellent speed and an intuitive user interface. Many newcomers are drawn to its particular talents and user-friendly features, making it a popular choice among programmers.

As a result, developers now design apps in a distinctive manner, giving them a competitive advantage over other developers. Because of MongoDB’s broad feature set, developers have been able to add cutting-edge functionality to their applications.

Accessing and modifying data in MongoDB is now easier than ever. You may also customize your queries, edits, printing, and data changes.

The power of MongoDB apps is widely underestimated. The database can handle more sophisticated CRUD operations. This database platform powers a variety of applications, including dashboard integrations, eCommerce systems, and review sites.

It’s wonderful to combine this database management solution with JavaScript frameworks like Nodejs. Reading, altering, and deleting data from dummy databases is common in online and software engineering. Furthermore, they are vital to the functioning of a multitude of applications.

Okay, enough of the pep talk. Let us cut to the chase. This guide will walk you through the easiest way to rename a collection in MongoDB with Nodejs. We will create a small application for the same and go about it.

Prerequisites:

  • Nodejs is installed on your machine
  • MongoDB Community version is installed on your machine
  • Have basic know-how of Nodejs, NPM and MongoDB

So, let us get started.

  • Open your Bash terminal. Create a Nodejs project directory and move into it:
mkdir renameCollectionProject

cd renameCollectionProject
  • Create a new Nodejs project and the entry point file:
npm init -y
touch index.js

You should now see a package.json and the index.js file created in your project directory.

  • Install all the required packages:
npm i mongodb

We will use the mongodb package to easily connect to the local MongoDB server.

  • Open the index.js file in your code editor and import the installed package(s):
const MongoClient = require("mongodb");
  • Start up the MongoDB server and open the Mongo shell
  • We will first check the name before we rename a collection in MongoDB. Navigate into the database you want to use:
show dbs

use randomDB
  • List collections to see the name:
show collections
products
  • Connecting to the local MongoDB server using the mongodb NPM package. We are also passing the code to rename a collection in MongoDB in the index.js file:
const MongoClient = require("mongodb");
const url = 'mongodb://localhost:27017/';
const dbName = "randomDB"; // Database name

MongoClient.connect(url).then((client) => {

	const connect = client.db(dbName);

	// Connecting to a collection
	const collection = connect
		.collection("products");

	// Renaming the collection name
	collection.rename("randomCollection");

	console.log("Collection renamed successfully!");
}).catch((err) => {
	console.log(`Uh Oh! An occurred while renaming a collection :(`);
	console.log(err.Message);
})
  • Run the file:
node index.js

The output:

Collection renamed successfully!

Checking the Collection Name

  • Jump to the mongo shell again
  • Go to the same database whose collection you renamed:
show dbs
use randomDB
  • Listing the collection to check if the code to rename a collection in MongoDB worked:
show collections
randomCollection

We have successfully learned how to rename a collection in MongoDB using Nodejs.

Read More: Ultimate Beginner’s Guide to Nodejs REPL in 2021

Conclusion

MongoDB’s numerous strong capabilities and functions are unparalleled. Modern NoSQL databases, such as MongoDB, provide excellent speed and an intuitive user interface. Many newcomers are drawn to its particular talents and user-friendly features, making it a popular choice among programmers.

This guide will walk you through the easiest way to rename a collection in MongoDB with Nodejs.

Aneesha S
Aneesha S
Articles: 172