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.
db.createCollection(name, options)   

MongoDB – Create Collection

Login to Mongo shell and select your database with the following command.
 use mydb
Now create your collection with createCollection() command. The below command will create collection named mycol in current database.
 db.createCollection("aodba")  

{ "ok" : 1 }

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.
 use mydb
 show collections

-- output 
aodba
....

MongoDB Rename Collection

Use db.collection.renameCollection() method to rename existing collection in MongoDB database. Syntax:
db.collection.renameCollection(target, dropTarget)
Example: For example, You have a collection “aodba“. Let’s use the following command on mongo Shell to correct collection name to “ao“.
db.AODBA.renameCollection("ao")
Output:
{ "ok" : 1 }


Leave a comment or a question in the comment area bellow if you fell like


Be a sport and

Search

Loading... Please wait

Subscribe to our Newsletter

Be a sport and

x