- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB query to rename a collection?
To rename a collection in MongoDB, use renameCollection(). Let us create a collection with documents −
> db.demo690.insertOne({_id:101,Name:"Sam"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo690.insertOne({_id:102,Name:"Mike"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo690.insertOne({_id:103,Name:"John"}); { "acknowledged" : true, "insertedId" : 103 }
Display all documents from a collection with the help of find() method −
> db.demo690.find();
This will produce the following output −
{ "_id" : 101, "Name" : "Sam" } { "_id" : 102, "Name" : "Mike" } { "_id" : 103, "Name" : "John" }
Following is the query to rename collection −
> db.demo690.renameCollection("demo691"); { "ok" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo691.find();
This will produce the following output −
{ "_id" : 101, "Name" : "Sam" } { "_id" : 102, "Name" : "Mike" } { "_id" : 103, "Name" : "John" }
Advertisements