MongoDB: Show Collections

In our previous tutorial, we have created a collection called comics inside the database called books. In this tutorial, we will learn how to view or display them.

In MongoDB, a collection is a grouping of documents and a database consists of many such collections. But a collection can belong to only one database.

To view all collections in a database, we first need to start both the Mongo Daemon and the Mongo Console.

If you have a Mac, open your terminal and start the Mongo Daemon.

				
					$ mongod
				
			

After that, again open a new terminal and start the Mongo Shell.

				
					$ mongo
				
			
mongod and mongo shell

Howevern, if you are using Windows, go to the /bin directory and double-click on the Mongo Daemon mongod.exe. And in the same /bin directory, double-click on mongo.exe next. This is the Mongo Shell from where you will be executing commands.

To view collections, you first need to switch to the database with the use command. The show collections command

				
					$ use books
					$ show collections
				
			

will list all collections belonging to the books database.

mongodb show collections