MongoDB – Create a Database

Overview – MongoDB Create Database

The MongoDB database is used to store all the collections. The collection is used to store all the documents & the document is used to store all the field name & field value related to an individual document.

In MongoDB, there is no command or statement of CREATE DATABASE like we do in SQL. you can create a database by simply switching to a non-existing database and to insert data into it’s one of the collection.

You can install MongoDB here if you don’t have.

See the code below that shows a basic example of how a document look likes.

{
  "userID" : 9,
  "userName" : "John Doe"
}

The above code block is a simple example of document Where “userID” & “userName” are field name and 9 & “Techy Hunger” are field value. This is just one document that will be inserted into a collection name “users”.

How to Create a Database in MongoDB?

If there is no existing database, use the following command to create one.

use employee

Using the above command if there is already an existing database named “employee” available then it will return that. But, If the database doesn’t exist, it will be created.

The result will print the following:

switched to db employee

However, the database is still not created if there is no database exist until you insert a single document into the collection.

Now, create a collection and insert a document into it using the following command:

db.users.insert({ "userID": 2, userName: "Techy Hunger" })

By running the above code one document will be inserted into a collection called “users”.

Let’s understand the above code.

  • If there is a database already exist, the use employee command use the database, but if there is no database available it will create the new one.
  • If there is a collection(table) already exist, it will insert the data (field name & value) into it. But, if not exist then it will create collection first, then insert the data.

After running the command it will print the message:

WriteResult({ "nInserted" : 1 })

After inserting data you can verify your newly created database and collection.

To do that you can list the all existing database using the following command:

show dbs

this will print the message:

local     0.000GB
employee  0.001GB
test      0.005GB

In the output result, you can find your own created database as we have found the “employee” database.

You can also view the inserted documents into the collection by following the command:

db.users.find()

and the output will be:

{ 
 "_id" : ObjectId("5940fbf976ef3c6b3ffb7857"),
 "userID": "2", 
 "userName" : "Techy Hunger"
}

You can see, there is a field _id which we did not provide but MongoDB creates it for you automatically if you provide or not.

Now, you can see we have successfully created a database named employee and inserted a document into users collection.

Finally, MongoDB Create Database tutorial is over hope you liked it. Please share your thoughts in the comment box.

 

 

 

 

 

Leave a Reply