Nowadays, organizations across the world have started adopting the NoSQL database to provide solutions to various business problems. NoSQL databases have gained popularity due to the features that they contain. There are multiple open source NoSQL databases that are available in the market and MongoDB is one among them 

What is MongoDB? 

MongoDB is defined as a NoSQL Open Source database that supports different forms of data. It is designed in a manner to support the large volume storage requirements of various organizations. It is a distributed database at its core and has inbuilt features such as high availability, geographic distribution, and horizontal scaling that make it simple to use. 

MongoDB show collection

MongoDB show collection is a command from the MongoDB shell that helps in listing the collections created in the current database.  To view the collection, you need to select the one that you want to view. 

If you want to list the available collections in MongoDB database, all you need to do is select the mydb, then run show collection command 

> use mydb
> show collections

Output 

Mindmajix
Students 
Leads 
Servies 
Employees 

Frequently Asked MongoDB Interview Questions & Answers

How to create a collection: 

We use the below-mentioned command to create a collection in the database: 

db.createcollection( name, options )

Subscribe to our youtube channel to get new updates..!

A collection is a group of MongoDB documents that exists within a table. 

Below mentioned are the steps to be followed to create a collection: 

  • Name: Give any name for the collection that you are going to create.  
  • Options: It is a document that acts as a configuration for the collection. 
  • Capped: It acts as a flag to mention. If you want to create a capped collection then we should need a capped collection with a predefined size.  If the size reaches the maximum extent then the MongoDB starts overwriting the entire collection to provide the accommodation to new documents into a collection. 
  • Size: If you mark a parameter cap as true, then the size argument is always required. One can even have to specify the max number of the parameter document. 
  • Max: It indicates the number of documents that are accepted in a capped collection. 

Checkout MongoDB Blogs

 

Method one: Creating a collection in MongoDB in MongoDB:  

Here in MongoDB, you need to insert a document before you are going to create a collection. Using a simple command, you can insert a document in the command collection and then the MongoDB create collection instantaneously. The syntax we use to insert a document is shown below.  

Syntax:

db.collection_name.insert({key:value, key:value…})

Let's consider an example here: 

We are not having a collection for studentsprofile in the studentprofileDB. We can easily create a collection named “studentsprofile” very quickly and we can insert a document by allocating keys and value pairs. 

> use studentsprofile 
Switched to db studentsprofile 
db.studentsprofile .insert({
   name: "Naveen",
   age:  21,
   website: "studentsprofile.com"
})

Now, you can see the command prompt as shown below. 

writeResult({"nInserted" : 1 }) 

Administrator: Command Prompt - mongo -

> db.beginnersbook.insert({
. . .  name: "Naveen",
. . .  age: 30,
. . .website: "studentsprofile.com"
. . . })
WritenResult ({ "nInserted" : 1 })
>

If you want to have a check over whether the document is successfully implemented or not, just type the following command. It will display you the documents in the list of collections. 

Syntax:

db.collection_name.find() 
> db.studentsprofile.find()
{ "_id" : objectId("48bc9d3524235cdb68ba77"), "name" : "Naveen",
  "age" : 30, "website" : "studentsprofile.com" } 

Use the below command to check whether the collection is created successfully or not.

Show collection 

The below-mentioned command shows the list of all the collections in the currently selected database.

> show collections
studentsprofile

Conclusion

You might have got a clear idea about what is MongoDB and how to create a collection in MongoDB. Hope this blog helped you in gaining some knowledge on show collection and command lines. 

Explore MongoDB Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download Now!