How to create database in MongoDB
How to create database in MongoDB

How To Create Database In MongoDB

In this tutorial we will learn how to create database in MongoDB. Create Database in MongoDB can be done with several available tools, namely:

  1. Using MongoDB Compass
  2. Using the MongoDB Shell
  3. Using the MongoDB Atlas UI

In this tutorial, we will only discuss how to create a database using MongoDB Compass and MongoDB Shell, while using the MongoDB Atlas UI is not discussed in the tutorial.

1. Create Database Using MongoDB Compass

In this stage, we will create a new database using MongoDB Compass with the following steps:

  1. Click “Create Database” to open the dialog
  2. Enter the name of the database and its first collection
  3. Click “Create Database
creat database in MongoDB Compass

On this example we will create a new database new called as : customer then a new collection with cust_details, the result is as shown below :

2. Create Database Using MongoDB Shell

Using the MongoDB shell, mongosh, allows us to interact with MongoDB through the CLI interface. By entering commands into the CLI, we tell MongoDB how to operate, get information about how the MongoDB cluster is running, and perform the basic action we’re going to cover today: creating a database.

Previously, MongoDB Shell installation can be seen in How To Install MongoDB Shell On Rocky Linux 8 article.

Syntax

The basic syntax to create new database in MonogDB is :

use <database_name>

Steps To Create Database Steps in MongoDB :

  • Step 1. List allthe available databases by using the show dbs command.
  • Step 2. Execute use command in the MongoDB shell.
  • Step 3. Check the list of databases again.

The explanation of the steps above are :

1. We will list all database which are already exist by submittig command line : show dbs, as shown below.

test> show dbs
admin     132.00 KiB
config    108.00 KiB
customer   48.00 KiB
local      72.00 KiB

2. Then we will create a new database with basic syntax :

use <database_name>

Example :

test> use supplier
switched to db supplier

Then we will create collection within new database which was created, in this example we will create a collection named as supp_details, by submitting command line :

db.supp_details.insert({name: "Aneka Caraka", address: "Pangkal Pinang"})
db.supp_details.insert({name: "IMN Mining", address: "Tidung Island"})

The result is as shown below :

supplier> db.supp_details.insert({name: "Aneka Caraka", address: "Pangkal Pinang"})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("63a8266da316caaf61bff380") }
}
supplier> db.supp_details.insert({name: "IMN Mining", address: "Tidung Island"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("63a8293ba316caaf61bff381") }
}

3. Then we will list the current database which was already created on MongoDB using command line : show dbs.

supplier> show dbs
admin 132.00 KiB
config 108.00 KiB
customer 48.00 KiB
local 72.00 KiB
supplier 40.00 KiB
list database on MongoDB

To list a collection we will use command line :

show collections

output :

supplier> show collections
supp_details

To display the content of a collection, we will use command :

db.<collection_name>.find()

output :

supplier> db.supp_details.find()
[
  {
    _id: ObjectId("63a8266da316caaf61bff380"),
    name: 'Aneka Caraka',
    address: 'Pangkal Pinang'
  },
  {
    _id: ObjectId("63a8293ba316caaf61bff381"),
    name: 'IMN Mining',
    address: 'Tidung Island'
  }
]

Until this stage, we have already create a new database on MongoDB.

Conclusion

We have learned how to create a new database in MongoDB by using : MongoDB Compass and MongoDB Shell. Then we have created a collection inside new created database. I hope this short tutorial will be helpful.

(Visited 238 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *