In Focus

Working With Databases In MongoDB

In this article, we will learn about Database in MongoDB. If you are new to MongoDB then I would recommend you to go through the previous article in this series.

In this article, we will learn about Databases in MongoDB. If you are new to MongoDB, then I would recommend you to go through the previous articles in this series.
Before starting, let’s see the terminologies we are going to use frequently in this write-up, as well as in the upcoming articles, of this series.
 
Database in MongoDB 
  • A field is a name-value pair and its concept is much similar to the Columns of RDBMS.
  • The document follows the JSON syntax known as BSON and it's quite similar to the Rows in RDBMS.
  • A collection is a group of documents in MongoDB and it is similar to the Tables in RDBMS.
  • A Database is a container for all the collections and a single node of MongoDB Server can store multiple databases.
In RDBMS, we use joins in order to get the data from the multiple tables, whereas in MongoDB, the data is stored in a single collection but separated by Embedded Documents.
 
Before creating a database, we need to connect the MongoDB Client with the use of a Mongo Shell command. We already saw how to do that in the previous article of the series.
 
Let’s begin.
 
db Command
 
The db command is used for checking the currently selected database. By default, it shows you the test database as the selected database.
 
Database in MongoDB 
 
show dbs
 
The show dbs command shows you the database list available on MongoDB.
 
Database in MongoDB 
 
There are several database names which are reserved and will be shown initially.
  1. admin
    If a user is added to the admin database, then the added user automatically inherits permission to access all the databases and also, helps to run the commands that can only run from the admin database.

  2. local
    A local database stores data used in the replication process. In replication, the local database stores internal replication data for each member of a replica set.

  3. config
use
 
The use command is used to create a new database if the database does not exist. And if it exists, then it will select the existing database.
 
Database in MongoDB
 
In the above example, we created a new database, i.e., MongoDBTutorial and after that, checked the active or currently selected database. But still, if you try to check the database list using the show dbs command, you will not be able to see the MongoDBTutorial database as we have not added any collection in that base. Now, add a collection (we will learn more about the collection, in depth, in the next article of the MongoDB series). We can add collection in a database with the use of createDatabase() method which accepts the name of the database as a parameter. After adding a collection, the database will be shown in the database list. 
 
Database in MongoDB 
 
dropDatabase()
 
In MongoDB, we can drop the database with the help of dropDatabase() method. Before dropping the database, make sure that you have selected the right database with the db command. As, for example, we first checked the selected database and then dropped it with dropDatabase() method. 
 
Database in MongoDB 
 
Hope this will help you.
 
Thanks.