Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Saturday 20 October 2018

Working with Database in MongoDB

In this article, we will learn about Database in MongoDB. Before starting, I would recommend you to go through the previous article in this series:



Before starting let’s see the terminologies we are going to use frequently in this as well as in the upcoming article in this series.
A field is a name-value pair and its concept is much similar to the Column of RDBMS.
Document follows the JSON syntax know as BSON and it's quite similar to the Rows in RDBMS.
A collection is a group of the Documents in MongoDB and is similar to the table 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 data from the multiple tables whereas, in MongoDB, data is stored in the single collection but separated by Embedded Documents.

Before starting, we need to connect 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: db command is used for checking the current selected database. By default, it will show you test Database as the selected database.
show dbs: show dbs command will show you the database list available on the MongoDB.
There are several database names which are reserved and will be shown initially are:
1. admin: If a user is added to the admin database, then added user automatically inherits permission to the all databases and also helps to run the commands that can only run from the admin database.
2. local: 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: use Command is used to create a new database if the database does not exist. And if exists, then it will select the existing database.
In the above example, we created a new database I.e. MongoDBTutorial and after that check the active or current selected database with the db command. But still, if you try to check the database list using the show dbs command, you will not able see that 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 the 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.
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 drop it with dropDatabase() method.
Hope this will help you.
Thanks.

2 comments:

  1. I am thankful to this blog giving unique and helpful knowledge about this topic. Web Design

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook