How to create a Database and perform CRUD operations in MongoDB through command prompt?

MongoDB is one of the most popular NoSQL databases . Instead of inserting rows into tables as in traditional relational databases you insert documents into collections in a MongoDB. The documents have the structure of a JSON.

And the documents dont have to have a defined structure as tables need to in a relational database. A single collection can have two documents with varied structures. Say , the first document has the name and age of an employee and the second document has the name,age and address of another employee.

This gives a lot of flexibility . It is easier to store JSON data without post processing in a MongoDB collection.

Let’s see how to start up with MongoDB.

Let’s see how to create a collection and do CRUD operations on it from the command prompt.

STEP 1: Download and Install MongoDB

First install MongoDB from here :

https://www.mongodb.com/try

Go for the default options while installing.

STEP 2: Create a folder in your machine to store the databases and let MongoDB know about that

Create a folder where MongoDB will store the databases and inform this to MongoDB.

This can be done as below:

First create a folder C://data/db:

C:\>md data
C:\data>md db

Go to bin folder of your MongoDB installation.

Below is the location in my machine:

C:\Program Files\MongoDB\Server\4.2\bin

You need to use two exe files:

  1. mongod.exe – This is the daemon process which creates database instances
  2. mongo.exe – This is the shell used to connect to databases and fire queries

First run mongod.exe by telling it the db path:

C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe --dbpath "C:\data\db"

This will start the daemon process.

Once set , the next time when you start the process you don’t need to specify the db path.

STEP 3: Start MongoDB Shell

Then open a new command prompt and fire mongo.exe

C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe 

This starts a shell which can be used to fire database commands.

STEP 4: Create a database

To create a database just type use DATABASE_NAME in the shell just opened .

This will create a new database if it doesn’t already exist or return existing one if it already there.

>use employeedb

A new database named employeedb is created using the above command.

STEP 5: Create a new collection

To create a new collection , start directly by inserting data into it. This will create the collection and insert the data as well:

> db.employee.insert({"name":"Vijay","age":34,"technology":"java"})

db refers to the current database (employeedb)

employee refers to the new collection being created

The data within the parenthesis is the JSON data to be inserted into the collection employee.

Let me insert one more document into the collection:

 db.employee.insert({"name":"Roy","age":30,"technology":"angular"})

STEP 6: Find all documents in a collection

To see all the documents in the collection thus created execute the below command:

db.employee.find()

It shows the following results:

{ "_id" : ObjectId("5ee34d9c5b5afe7688916fb8"), "name" : "Vijay", "age" : 34, "technology" : "java" }
{ "_id" : ObjectId("5ee34da85b5afe7688916fb9"), "name" : "Roy", "age" : 30, "technology" : "angular" }

MongoDB automatically creates unique Object Identifiers – ObjectId() while inserting the documents.

To find a particular document , say document with the name “Vijay” , pass the criterion as a parameter to find() method:

 db.employee.find({"name":"Vijay"})

This lists out the below result:

{ "_id" : ObjectId("5ee34d9c5b5afe7688916fb8"), "name" : "Vijay", "age" : 34, "technology" : "java" }

STEP 7: Update a document in a collection

To update a document call update command by passing the criterion and setting the new value using $set keyword:

> db.employee.update({"name":"Vijay"},{$set:{"age":32}})

The above command updates the age field of the first document matching with the name Vijay to the new value.

If you list out the documents again , you can see the age value updated for the document with name “Vijay”:

> db.employee.find()
{ "_id" : ObjectId("5ee350554ccb93b1011ba07b"), "name" : "Vijay", "age" : 32, "technology" : "java" }
{ "_id" : ObjectId("5ee3506a4ccb93b1011ba07c"), "name" : "Roy", "age" : 30, "technology" : "angular" }

As mentioned the update command by default updates the first matching document. To update multiple documents at the same time add one more JSON object to the update document command : {multi:true}

> db.employee.update({"name":"Vijay"},{$set:{"age":33}},{multi:true})

STEP 8: Delete a document in a collection

To delete a document use the remove() command and pass the criterion as a parameter:

> db.employee.remove({"name":"Vijay"})

This removes the document with the name matching “Vijay”

> db.employee.find()
{ "_id" : ObjectId("5ee3506a4ccb93b1011ba07c"), "name" : "Roy", "age" : 30, "technology" : "angular" }

That’s it!

We saw how to create a MongoDB database and perform CRUD operation on the documents using command line in this post.


Posted

in

by

Comments

One response to “How to create a Database and perform CRUD operations in MongoDB through command prompt?”

  1. […] Creating a database in Mongo DB and performing CRUD operations […]

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading