Home  >  Blog  >   MongoDB

MongoDB Show Collections

Rating: 4
  
 
44402

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. 

If you want to enrich your career and become a professional in MongoDB, then visit Mindmajix - a global online training platform: "MongoDB Training" This course will help you to achieve excellence in this domain. 

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 the MongoDB database, all you need to do is select the mydb, then run the show collection command 

Related blog: Checkout MongoDB Tutorial

> use mydb
> show collections

Output 

Mindmajix
Students 
Leads 
Servies 
Employees 

Frequently Asked MongoDB Interview Questions

How to create a collection: 

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

db.createcollection( name, options )

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. 

MindMajix YouTube Channel

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.  

Related blog: Create MongoDB Collection

Syntax:

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

Let's consider an example here: 

We are not having a collection of student's profiles 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. 

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
MongoDB Training Apr 20 to May 05View Details
MongoDB Training Apr 23 to May 08View Details
MongoDB Training Apr 27 to May 12View Details
MongoDB Training Apr 30 to May 15View Details
Last updated: 03 Apr 2023
About Author

Vinod M is a Big data expert writer at Mindmajix and contributes in-depth articles on various Big Data Technologies. He also has experience in writing for Docker, Hadoop, Microservices, Commvault, and few BI tools. You can be in touch with him via LinkedIn and Twitter.

read more