MongoDB: How to create Database and Collection.

Introduction

In this tutorial, we will learn “How to create Database and Collection in MongoDB”. The collection means the table as in SQL Databases.

Tables contain rows and columns, but the collection contains documents in key-value pairs.

MongoDB is a No-SQL database which is written in C++, It uses a JSON like structure. MongoDB is a cross-platform and document-oriented database.

The initial release of the MongoDB was on 11 February 2009, you can find the main website of the MongoDB as well the git repository.

So we are going to discuss the following points,

  1. How to create database and collection in MongoDB.
  2. Insert your first data into the collection.
  3. How to drop the database in MongoDB.
  4. Create and delete the database via MongoDB compass tool.

How to Create database and collection in MongoDB.

So creating a database is a very simple task, the syntax will be like “use DATABASE_NAME”.

Syntax:

> use DATABASE _NAME
Example:

> use tastethelinux
switched to db tastethelinux
>

You can also use the “db function” to check whether you are in the correct database or not.

> db
tastethelinux
> 

So we have switched the database to tastethelinux, now let’s create the collection.

> 
> db.createCollection('linux');
{ "ok" : 1 }
> 

So the collection got created with the name “linux’ but still, we don’t have any data in our collection.

> show dbs
admin          0.000GB
config         0.000GB
local          0.000GB
tastethelinux  0.000GB
> 
> show collections
linux
> 

Show collections will list the collections that are present in the database.


How to Insert data in MongoDB.

2 ways to insert the data, while switching the database or create the collection and then insert the data.

> 
> db.linux.insert(
... {
... "Name": "Ashish",
... "Website": "tastethelinux.com",
... "Age": 27
... }
... );
WriteResult({ "nInserted" : 1 })
> 
> 

So we have inserted data with 3 key-value pairs Name, Website, and Age. You can also use the find function to check the inserted data.

> db.linux.find().pretty();
{
"_id" : ObjectId("626635fa790db5e4b9b8374f"),
"Name" : "Ashish",
"Website" : "tastethelinux.com",
"Age" : 27
}
>
>

So we have used a pretty() function to beautify the JSON structure document in MongoDB.


How to drop Database using db.dropDatabase() function in MongoDB.

To drop any database be careful, it may have important data. So if it is of no use then only delete the database.

let’s switch to the database and then use the db.dropDatabase() function.

> 
> use tastelinux
switched to db tastelinux
>
> 
> db.dropDatabase();
{ "ok" : 1 }
> 

So to check whether the database has dropped or not use the “show dbs” function in the MongoDB shell.

> 
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> 

How to create and Insert data in database via MongoDB Compass.

Developers, DBA use the MongoDB compass to manage there daily activities. We can import and export the database with the help of MongoDB compass.

Lets open the MongoDB compass application and then create the database and collections.

To create database in MongoDB compass

Click on “CREATE DATABASE” to create the database in compass. If you want to know how to connect to MongoDB Server then refer to the link.

Enter the Database Name and collection Name and click on Create Database in MongoDB Compass.

So put the name of the database and collection, and click on “CREATE DATABASE”.

So the database “tastethelinux” and collection “linux” has created. Now, it’s time to insert data into the collection.

Click on Insert Document in MongoDB Compass.

Click on “Insert Document” to insert the data. Suppose you have a JSON structure file or the backup file then you can import via MongoDB Compass.

Inserting Document in the linux collection.

So we have considered the 3 key-value pairs Name, Website, and the Age then click on “INSERT”.

Data got inserted into the collection.

After inserting you can find the data with the object_ID created. It is to identify unique data and act as a primary key in the collection.

Drop the database via MongoDB Compass.

Suppose you want to DROP the database then click on the “ICON” and it will prompt you to drop the database.

I love compass because it makes my life easier to manage the database for MongoDB.


Give your valuable time