Create Database in MongoDB - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
Create Database in MongoDB

Create Database in MongoDB

Create Database in MongoDB

In this tutorial, we will see how to create and use a database in MongoDB.

MongoDB Create Database

Start the MongoDB service by typing this command:
net start MongoDB
You should see a message like this:
The MongoDB service is starting..
The MongoDB service was started successfully.
Navigate to the bin directory of MongoDB as shown in the screenshot below:
Now we should be working in the MongoDB shell. To run the MongoDB shell, type the following command:
mongo
Once you are in the MongoDB shell, create the database in MongoDB by typing this command:
use database_name
For example I am creating a database “beginnersbook” so the command should be:
use mycareerrepublic
Note: If the database name you mentioned is already present then this command will connect you to the database. However if the database doesn’t exist then this will create the database with the given name and connect you to it.
At any point if you want to check the currently connected database just type the command db. This command will show the database name to which you are currently connected. This is really helpful command when you are working with several databases so that before creating a collection or inserting a document in database, you may want to ensure that you are in the right database.
> db
mycareerrepublic
To list down all the databases, use the command show dbs. This command lists down all the databases and their size on the disk.
> show dbs
admin
0.000GB
local 0.000GB
show dbs command list all databases
As you can see that the database “beginnersbook” that we have created is not present in the list of all the databases. This is because a database is not created until you save a document in it
Now we are creating a collection user and inserting a document in it.
We will learn how to create collection and document in the next tutorials.
> db.user.insert({name: "Chaitanya", age: 30})
WriteResult({ "nInserted" : 1 })
> show dbs
admin
0.000GB
beginnersbook
0.000GB
local 0.000GB

You can now see that the database “mycareerrepublic” is created.

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.