How to Create/List and Rename a MongoDB Collection a
What is a MongoDB Collection
A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection have a similar or related purpose.
Syntax
Use db.createCollection() command to create collection. You need to specify the name of collection, Also can provide some optional options like memory size and indexing.
MongoDB – Create Collection
Login to Mongo shell and select your database with the following command.
Now create your collection with createCollection() command. The below command will create collection named mycol in current database.
MongoDB – Show Collection
Use show collections command from MongoDB shell to list all collection created in the current database. First, select the database you want to view the collection.
MongoDB Rename Collection
Use db.collection.renameCollection() method to rename existing collection in MongoDB database.
Syntax:
Example:
For example, You have a collection “aodba“. Let’s use the following command on mongo Shell to correct collection name to “ao“.
Output: