Blog

MongoDB Show Collections

  • (4.0)

MongoDB Show Collections in this article we will walk you through what Collections are in terms of MongoDB database server, the key concepts and fundamentals of databases, collections, documents in MongoDB.

MongoDB Show Collections What Is It ?

After going through the recent articles on MongoDB installation, MongoDB GUI tools and Mongo Create database articles, you should have a fair bit of idea about MongoDB as such. Having said that, let us discuss how different is MongoDB (a NoSQL database server as against a traditional RDBMS database server).

Accelerate Your career with MongoDB Training and become expertise in MongoDB. Enroll For Free MongoDB Training Demo!

Since that we are going out of the traditional database nomenclature, let us revisit the nomenclature of MongoDB now and the next section is all about this.

MongoDB Vs Traditional SQL:

The differences between a MongoDB and a traditional SQL are very minimal for a beginner to understand, and is easy enough. The basic terminology used differs between MongoDB and SQL and here is the listing of the same.

 MongoDB terms  SQL terms
 Database  Database
 Collection  Table
 Document  Row
 Field  Column
 Primary Key  Primary Key

Now with this knowledge, let’s get started slowly but steadily so that the readers wouldn’t have any trouble understanding the concepts or fundamentals.

Usage of Collections:

Let us now understand the concept of Collections in the MongoDB world. Collections are Tables in comparison to SQL world and any data that needs to be held on a database, should go under a Collection. Now that been said, let us look at how can create a collection first. The following command will help you create a collection with the name specified, but there are many options available for us to use while creating a collection as such – which we will discuss in detail in this section.

Let’s take a look at the syntax on how to create a collection under a database CreateCollectionExampleDB which is created just for this purpose. I’ll speed up the process of creating the database as this process is already explained earlier, screenshot follows:

Create MongoDB Collections

The command to use to create a collection in the database that we have just created in the step above, is as follows:

db.createCollection(name, options)

  • name – can be any name that you want for your Collection object that we are going to create

  • options is a document to specify the configuration for your collection, and it is absolutely an optional parameter to have. Adding references about the most commonly used options here in this section about capped collections.

    • capped – This is a flag used to mention, if we want to create a ‘Capped Collection’ which in turn means that there is a Collection object created with a specific size, and if the size of the object is reached then MongoDB starts overwriting the older entries of the Collection to accommodate newer documents into the Collection.

    • size – The size argument is always required when the parameter capped is marked true. This is required even when you specify the parameter max number of documents.

    • max – This parameter specifies the number of documents that are allowed in a capped collection.

Frequently Asked MongoDB Interview Questions & Answers

Let us see how we can create collections using the command explained above, we will try to create a normal collection and also a capped collection to see the difference between these.

Let us how to create a normal collection, we will name it ‘NormalCollection’.

MongoDB NormalCollection

Syntax:

db.createCollection(“NormalCollection”)

Now let us see how to create a capped collection and we will name it ‘MyCappedCollection

MongoDB - MyCappedCollection

Syntax:

db.createCollection( “MyCappedCollection”, { capped : true, size : 10000, max : 5000} )

The Collections created (either normal collection or capped collection), will be listed using the command show collections. Let us discuss more in detail about using this command in the section below.

Checkout MongoDB Blogs

Usage of command show collections:

The command show collections lists all the collections that are available for a specific database. Let us work upon the syntax of the command, shown below:

show collections [IN ] [FOR COLLECTION ] [NAMES_ONLY]

NAMES_ONLY option retrieves the collections names alone skipping the storage details of the collections in the result set of the command above.

For example, if we want to retrieve the collections available for a specific database then the following command is useful. The parameter is an optional parameter, including it in the command or not the result set will always contain the number of collections available in a specific database.

MongoDB NAMES_ONLY

Alternatives for the command above are as follows (there is no major difference when these other alternatives are used as well):

  • Show Tables

The command when used on a MongoDB shell retrieves the same results as the command show collections, but the usage of tables in MongoDB perspective will be confusing. Hence it is better to always stick with the collections nomenclature.

Tables in MongoDB

?db.getCollectionNames()

The command when used on a MongoDB shell retrieves the same results shown as above, but with only one difference – the result set is a list of strings. Let’s take a look at this result, screenshot follows.

Retrieving Tables in MongoDB

Conclusion:

In this article, we have learned how to create a collection under a database. We have also learned how to create a capped collection and learned what options are required to create one.

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

 

We have also learned about MongoDB commands to list all the collections available under a database and also understood the syntax of the same.


Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules