MongoDB is a NoSQL open-source document storage database. MongoDB is different from various other databases as it is a NoSQL variant and the best amongst the NoSQL variants too. MongoDB is written in C++ and is the leading NoSQL database in the open-source market. NoSQL as such is very different from the traditional RDBMS database systems, as they don’t follow the RDBMS principles of normalized tables, relationships and etc.
MongoDB comes with a plain installation with nothing much needed ahead of the installation. Once you install MongoDB on your system, you can either access the database instance via the MongoDB shell itself or you can rely on any of the best possible GUI tools for MongoDB. If you are a Windows user and if you want to use a GUI tool then we suggest that you use MongoBooster and if you are Mac OSX user and if you want to use a GUI tool instead of the MongoDB shell itself then we suggest that you use RoboMongo.
At the time of this writing, we have used MongoDB 3.4.7 on a Windows 7 machine with MongoBooster as the GUI tool to access the MongoDB database instance.
Creation of a database on MongoDB is a very simple process, in either of the cases - using the MongoDB shell itself or if you are using the MongoBooster GUI tool for Windows or RoboMongo GUI tool for Mac OSX platforms.
Let us start this process with the MongoDB shell itself, though you may want to use a GUI tool for MongoDB – it is always preferable to understand the semantics of MongoDB, the commands that come into play for achieving this to be done. GUI tool runs the same commands on the MongoDB shell where we as end-users wouldn’t check completely.
Connect to the MongoDB instance by running the MongoDB windows service or as in this case, I am willing to trigger the MongoDB service from a command prompt as an Administrator.
Once the MongoDB service is started on an administrator command prompt, you will have to run the following command to invoke the MongoDB shell to interact with the MongoDB databases as such.
show dbs command will show you what are the databases available for you to use when you connect to MongoDB shell. For our case right now, since there are no databases that we have created until now – it just shows ‘admin’ and ‘local’ which are the databases that MongoDB handles for its own administrative tasks.
Now for us to create a database on MongoDB shell, we need to use the use keyword along with a database name. MongoDB hasn’t provided any command to create a database, but this will be created on the fly when you create a document under a Collection by default. But then we can go ahead and use the use keyword for the database creation. It works this way, if the database doesn’t exist then it creates it and if it exists it uses the database without giving any errors.
See the above screenshot, where show dbs command is still isn’t able to identify the newly created database ‘CreateDatabaseExample’. It is because MongoDB doesn’t save it until you create a document under a collection under that database. Now let’s try creating a simple document in a collection in our database ‘CreateDatabaseExample’.
After creating a document under the database, the show dbs command started listing the database that we have created earlier. If you wanted to check which database you’re currently connected to, then you can issue just the following command (db) and see to which database you’re connected to. Screenshot follows:
Now if we want to do the same from the MongoBooster GUI tool, it is just a simple process of creating a new database via an interactive way. Let’s check that out too, for that give your connection details (hostname should be the server IP where the database is hosted and an empty port for MongoDB to run and also for the MongoBooster to interact with) on MongoBooster and connect.
The first step is to right click on the connection that you have created for your MongoDB instance on MongoBooster (here in our case, named it localhost and the MongoDB was hosted locally as well)
On clicking on ‘Create Database’, MongoBooster opens up an alert box to provide a name to our database. Name it ‘CreateDBViaGUI’ and say OK to create the database. Screenshot follows:
This will create the database and shows on the MongoBooster GUI readily but for MongoDB to save the database, you need to insert at least one document in the database that you’ve created.
We have understood what MongoDB is and also understood how it fares against the traditional RDBMS database systems. Creating a database is a very simple process but understanding the specifics that run on the underlying MongoDB shell is very important and that is what we targeted through this article. We were also able to justify the need for a GUI for the MongoDB shell to perform actions in very easy steps.
Get Updates on Tech posts, Interview & Certification questions and training schedules