EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Database
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

MongoDB Database

By Priya PedamkarPriya Pedamkar

MongoDB Database

Introduction to MongoDB Database

MongoDB database is defined as create database for database operations, using MongoDB database we are creating the collection into it. MongoDB is not structured database so we have not used create database statement which we have used in other databases like MySQL and PostgreSQL. We can create the database in MongoDB by using the database, use keyword, it is used to create new database in MongoDB, after using the database we are creating the collection into the same. We can create multiple database on single MongoDB server default database which we have used in MongoDB is db.

How to Create Database in MongoDB?

  • Basically we have used use command to create new database in MongoDB.

Below is the syntax :

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Use name_of_database

  • In above syntax use keyword specifies that create new database. We can create any new database by using use keyword.
  • Name of the database shows that database name which we are creating in MongoDB. We can create database by specifying the name of database parameter.
  • If using database is exist on the server it will use the existing database instead of creating the new database.

Below example shows that create new database:

Code:

use db_test
db

Output:

MongoDB Database 1

  • In above example we have created database name as db_test after creating we can see the connected database name by using db command.
  • We can show all the created and system database by using the show dbs command.

The below example shows that list  as follows:

Code:

show dbs

Output:

MongoDB Database 2

  • After using database it will not show in show dbs command, after creating collection or object of the database then it will show using show dbs command.

Below example shows the same:

Code:

use db_test1
show dbs
db.test.insert({"name":"ABC"})
show dbs

Output:

MongoDB Database 3

  • In above first example we have created database name as db_test1 it will show connected database by using db command but not showing in the list of database, because we have not created any collection or object into db_test1 database.
  • In second example we have created one collection at the time of data insertion after creating collection into the db_test1 database, name of database is shown using the show dbs command.
  • Next time after using the use db_test1 command we have automatically connected to the db_test1 database.
  • Admin and local databases are the system database which was used in MongoDB server.
  • Show dbs command will show all database name and size of all databases.

How to Alter Database in MongoDB?

Below syntax and example shows that how to alter database:

  •  We are using copyDatabase command to rename database.

Syntax:

db.copyDatabase ('name_of_old_database', 'new_database_name')
use name_of_old_database
db.dropDatabase();

  • In above syntax copyDatabase is defined as the command used to copy one database to other database with different name.
  • Name of old database is defined as old database name which we have used to copying database to new name.
  • Name of new database is defined as new database name which we are renaming using the copyDatabase command.
  • We cannot directly rename the database, to rename the database we are using copyDatabase command.
  • After copying to the database we are connecting to the old database and after we have deleting old database.

Below example shows that alter database command to rename the database with new name:

Code:

show dbs
db.copyDatabase('db_test', 'db_test1')
show dbs
use db_test
db.dropDatabase()
show dbs

Output:

Alter

  • In above example first we have copied database from db_test to db_test1 database. After successful copy from db_test to db_test1 we have connected to db_test database.
  • After connecting we have dropped old database using dropDatabase command.
  • Basically there is no rename database command is available in MongoDB so instead of rename database command we are using copyDatabase.

How to Delete Database in MongoDB?

Below syntax and example shows that how to delete the database:

  • We have drop the database in MongoDB using dropDatabase command.

Below syntax shows to drop the database, first we need to connect the specified database which was we are dropping.

Syntax:

use name_of_database
db.dropDatabase ()

  • In above syntax name of the database is defined as database name which we are dropping from the server.
  • Drop database command is used to delete existing database.

Given below example shows delete database:

Code:

show dbs
use db_test1
db.dropDatabase ()
show dbs

Output:

Delete

  • In above example we have deleted database name as db_test1.
  • To delete database we need to connect to the specified database which we are dropping in MongoDB.
  • The connected database will be deleted from database server, so after using this command we need to check the database name which we have connected.

Below example shows the same:

Code:

show dbs
use db_test1
db.dropDatabase ()
show dbs

Output:

MongoDB Database 6

  • In above example first we have connected to the db_test database after connecting we have deleted the same from the database server.

Conclusion

We are creating the database by using the use db command, if the database already existed in database then we are connecting to the existing database. We are renaming the database by using the copyDatabase command, also we can drop the database by using dropDatabase command.

Recommended Articles

This is a guide to MongoDB Database. Here we discuss how to create, alter, delete databases in MongoDB with respective query examples. You may also have a look at the following articles to learn more –

  1. MongoDB Timestamp
  2. MongoDB Relationships
  3. MongoDB Skip()
  4. MongoDB Features
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more