CRUD operation in mongoDB using Terminal/MongoShell
By: Muhammad Zain Tariq
Tags: Mongo Shell, Crud Operations, Crud, Mongodb

mongoDB

In this Blog, You will Learn

What is Collection and Documents?

CRUD operation in mongoDB.

If you can want to know about setup/installation of mongoDB you can check out my this blog Getting Start with MongoDB.

What is Collection and Documents ?

Tables in SQL database are called collections in mongoDB.

Records in a table of sql database is called documents.

CRUD operation in mongoDB.

Create a database in mongoDB

Now we will create our first database in mongoDB so first we will check which database our present in our system.

First Run mongoDB in our terminal writing following command in terminal

mongo

mongoDB run’s

now we will write following command for check already exist databases

show dbs

As you saw in figure 1. it show already created database zain database and 3 default databases

figure 1.

Now we will create our own database with the name of practice we will write following Command

use crudpractice

by this we create a database because of this command if a database is already present so we will use it or otherwise it will be created and then use it.

But when we check all databases using “show dbs” we will not be able to see our database even our database is created until their is no data we cannot see it.

Now we will do CRUD operation on this database for using it

Create or insert Data

In mongoDB their are two operations for data insertion which are

insertOne insertMany

InsertOne

This is used to insert only one document in a collection as we do in following figure by putting three values name ,age and degree for a document in a collection named data (it is created automatically when written as I created in terminal) syntax for this is

db.[collectionname].insertOne({docuemnt data[Separated with comma]})

here db is by default the database which we are using

InsertMany

This is used to insert more than one document in a collection as we do in figure below by putting more than one document in a collection named data syntax for this is

db.[collectionname].insertMany([{docuemnt1},{document2},....])

below we put 2 documents in our data collection

two documents

Read in mongoDB

For reading data from a database we will use following command

db.collection.find()

so for checking our present collection we can use following command

show collections

Now lets read our document from our collection named data.

here you can check that it provides use with all document in our collection

but their is a problem the documents are not arranged in a pretty structured mannar for this we will use following command

db.collection.find().pretty()

now look it is called well structured data.

But we can access our documents by their values as well but it is not case sensitive as shown in figure below for accessing documents where name is zain.

Update in mongoDB

now for updating documents in a collection named data we will perform two following commands

updateOne

This is used for updating only one document of a collection as we change age of zain to 22 in the figure below

hence we check age is changed.

updateMany

This is used to update multiple values of documents of a collection

Now we will change all degree values from BSCS to BSEE in left is the original value and in right is updated value in below figure.