Blog

MongoDB Show Collections

(4.0)

| 1813 Ratings |

Last Updated August 21, 2017

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 experts 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:

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 createoptions 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 as 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.

Usage of command show collections:

The command show collections list 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.

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.

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.

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.

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

Related Articles

Mindmajix - Online global training platform connecting individuals with the best trainers around the globe. With the diverse range of courses, Training Materials, Resume formats and On Job Support, we have it all covered to get into IT Career. Instructor Led Training - Made easy.