MongoDB

MongoDB List Collections

The collections in MongoDB are the type of entity that stores the JSON documents similar to the table in an RDBMS database. Sometimes, we encounter such situations within the MongoDB where we need to get a list of all the collections that are present in the specified database. However, a single MongoDB command makes it simple to access the list of all the available collections. We have different commands of MongoDB which are deployed to get the names of the collections within the existing database. This MongoDB article provides five different approaches to give the list of present collections.

How to Fetch the List of All the Collections in MongoDB

We must first generate the collection in the MongoDB database to list every collection there. Then, we can apply the commands to list all the collections in that specific database of MongoDB. The first step that is required to create the collections in MongoDB is to establish the database connection in the shell database. We utilize the “use” command with the name of the database as “University”. The query command to create the “University” database is represented in the following:

> use University

After giving the query to the MongoDB shell and upon entering the formation of the database query, output the following statement:

switched to db University

Now, the database is created successfully in MongoDB. We can use this database to insert the different collections. The query to create the collection in the database is as follows where we first create the “Faculty” collection. The createCollection() method takes the collection name, “Faculty”.

> db.createCollection("Faculty")
{ ok: 1 }

After that, we create another collection which is entitled “Teachers” by employing the createCollection() method of MongoDB. The output confirms that the specified collection is created in the “University” database.

> db.createCollection("Teachers")
{ ok: 1 }

Next, we create the third collection which is “Students” in the same “University” database. This indicates that a database can be inserted with many collections in MongoDB.

> db.createCollection("Students")
{ ok: 1 }

Example 1: MongoDB List Collection Using the “Show Collections” Command

The “show collections” is the shell command of MongoDB to access a set of MongoDB collections. The collections that we add to a MongoDB database are listed by this command. We execute the “show collections” command in our MongoDB shell which generates all the collections list in the output that we created in the current “University” database. The following is a representation of the “show collection” command:

> show collections

The command result is shown as follows:

Faculty

Students

Teachers

Example 2: MongoDB List Collection Using the “Show Tables” Command

The “show tables” command is another technique to get a list of the collections within a specific database. The approach that this command operates is quite similar to the working of the “show tables”. The command of the “show table” is provided in the shell of MongoDB to show the available collection in the “University” database.

>show tables

The output of the command lists three collections in the currently specified MongoDB database:

Faculty

Students

Teachers

Example 3: MongoDB List Collection Using the “GetCollectionNames()” Command

The previous two ways to list the collection in MongoDB are very common and easy to utilize in the MongoDB shell. Now, we have the db.getCollectionNames() command to generate the list of the existing collections in the database. The db.getCollectionNames() command provides an array that contains the names of each collection and view in the present database. Additionally, the collections are chosen by the user’s authority when employing the access permissions. So, here is the db.getCollectionNames() command example to show how this command fetches the collections in the MongoDB shell:

> db.getCollectionNames();

The collections are listed in the form of an array after executing the command in the shell.

 [ 'Faculty', 'Teachers', 'Students' ]

Example 4: MongoDB List Collection Using the “GetCollectionInfos()” Command

The next method of getting the collection list is using the db.getCollectionInfos() command in the MongoDB shell. The db.getCollectionInfos() command also generates the array output which includes the name and options with the documents that contain the collection or view details for the given database. We have given a db.getCollectionInfos() command on the MongoDB shell without any argument.

> db.getCollectionInfos();

The db.getCollectionInfos() command without any parameter value outputs the collections in the following way:

[
  {
    name: 'Faculty',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("105843b2-a0f4-412b-915a-16f984a0c78f")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  },
  {
    name: 'Teachers',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("61fe78f5-f1a4-4e72-bf3b-7df1f5b2e684")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  },
  {
    name: 'Students',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("78c03549-72e0-4d08-a73d-aea2eb2bfdf5")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  }
]

Now, we pass the parameter in the db.getCollectionInfos() command. The command in general takes the “filter” parameter which sets the query expression to filter the present collections of the database. Then, the “nameOnly” is the second parameter that specifies only the collection names from the db.getCollectionInfos() method. The “authorizedCollections” is also another parameter that enables the execution of the command with access control without the privilege requirement when the “nameOnly” argument is set to true. The “db.getCollectionInfos()” command with the previously-discussed parameter is given as follows:

> db.getCollectionInfos( {}, true, true )

The following screenshot shows the response to the aforementioned query:

[
  {
    name: 'Faculty',
    type: 'collection'
    },
  {
    name: 'Teachers',
    type: 'collection'  
  },
  {
    name: 'Students',
    type: 'collection'
  }
]

The db.getCollectionInfos() can only be passed with the “nameOnly” parameter as we provided in the following:

> db.getCollectionInfos( { name: "Teachers" }, true, true )

The results of removing the two arguments from the db.getCollectionInfos() method are represented as follows:

[
  {
    name: 'Faculty',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("105843b2-a0f4-412b-915a-16f984a0c78f")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  }
]

Example 5: MongoDB List Collection Using the “ListCollections()” Command

The listCollections() command also provides the information as a document. The database’s collections name and options are returned by the listCollections command. The “listCollections” is executed in the MongoDB shell as a parameter of the db.runCommand(). Furthermore, we also set the “authorizedCollections” and the “nameOnly” parameters with the true value.

> db.runCommand( { listCollections: 1, authorizedCollections: true, nameOnly: true } )

The results contain the details to create a cursor for data collection:

{
  cursor: {
    id: Long("0"),
    ns: 'University.$cmd.listCollections',
    firstBatch: [
      { name: 'Faculty', type: 'collection' },
      { name: 'Teachers', type: 'collection' },
      { name: 'Students', type: 'collection' }
    ]
  },
  ok: 1
}

Moreover, the following is an additional way to implement the listsCollection command:

> db.runCommand( { listCollections: 1.0 } )

The implementation of the listsCollection command provides a lot of information regarding the collections as displayed in the following:

{
  cursor: {
    id: Long("0"),
    ns: 'University.$cmd.listCollections',
    firstBatch: [
      {
        name: 'Faculty',
        type: 'collection',
        options: {},
        info: {
          readOnly: false,
          uuid: new UUID("105843b2-a0f4-412b-915a-16f984a0c78f")
        },
        idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
      },
      {
        name: 'Teachers',
        type: 'collection',
        options: {},
        info: {
          readOnly: false,
          uuid: new UUID("61fe78f5-f1a4-4e72-bf3b-7df1f5b2e684")
        },
        idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
      },
      {
        name: 'Students',
        type: 'collection',
        options: {},
        info: {
          readOnly: false,
          uuid: new UUID("78c03549-72e0-4d08-a73d-aea2eb2bfdf5")
        },
        idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
      }
    ]
  },
  ok: 1
}

Conclusion

We learned about the different ways to get a list of the collections that are found in a specific database in this post using the MongoDB shell. The first two commands, “show collections” and “show tables”, to get the collection list in the shell are commonly used by the user. It simply returned the list of the collections upon running the commands on the MongoDB shell. Then, we have the “getCollectionNames” command which lists all the collections in an array structure. Next, we employed the getCollectionInfos and the “listCollections” command which also generate the list of the collection and the information about the retrieved collections.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.