MLearning.ai
Published in

MLearning.ai

CURD Operations in MongoDB — NOSQL Database

Hey guys,

In this article I am going present Create, Read, Update, and Delete (CURD) operation with examples which you can perform in MongoDB. Mongo DB is a document based NOSQL database which is quite popular in the industry. This article will create a basic understanding of how to perform CURD operations in MongoDB.

So, lets dive into deep…

In first step launch MongoDB. If you want to know how to install and run MongoDB, you can refer to my article Introduction to MongoDB — a NoSQL Database (How to Install — MongoDB on Ubuntu?)

1. Create Operation:

Frist I will create a dataset. To create a database run following command:

> use studentdb

To create collection in MongoDB, run the following command:

> db.createCollection(“midterm”)

Once the collection is created, I can insert documents into the collection. To insert documents into the collection, run the following command:

# Insert record in collection

> db.midterm.insert(

{

_id: 1,

name:”Ayush”,

gpa:5.5,

DOA:”01-Jan-2022"

}

);

To insert more than one document or bulk documents, run the following command:

db.midterm.insert(

[

{

_id: 2,

name:”Jay”,

gpa:5.8,

DOA:”02-Jan-2022"

},

{

_id: 3,

name:”Aakash”,

gpa:8.5,

DOA:”02-Jan-2022"

}

]

);

2. Update

MongoDB allows to update documents inserted in the collection. To update existing document in the collection, run the following command:

>db.midterm.update( {_id: 3}, {$set:{gpa:2.5}});

In case of the document does not exists in the collection, it will be inserted otherwise updated. Use the following command to update record

db.midterm.update(

{

_id: 5,

name:”Rohit”,

gpa:6.8,

DOA:”02-Jan-2022"

},

{$set:{gpa:6.5}

}, {upsert:true}

);

To add an additional attribute in the collection, run the following command:

db.midterm.update( {_id: 3}, {$set:{class:”BDA”}});

3. Read Operation:

To read documents form collection is very simple in the MongoDB. The find() method allows to read / show records available in the collection.

>db.midterm.find();

To format read result, we can use pretty() method

>db.midterm.find().pretty();

To find specific document in the collection, run the following command:

>db.midterm.find({name:”Ayush”}).pretty()

To display only the name from all the documents from collection midterm and identifier id should not be displayed, run the following command:

>db.midterm.find({}, {name:1, _id:0}).pretty()

Here, _id:0 indicate that the _id will not be included in the result.

>db.midterm.find({}, {name:1,gpa:1, _id:0}).pretty()

>db.midterm.find({_id:1}, {name:1,gpa:1}).pretty()

You can use conditional operator to perform desired output based on satisfying the search criteria. The Relational operators are

To select name, gpa from midterm collection whose id is 1, run the following command:

>db.midterm.find({_id:1}, {{},name:1, gpa:1})

>db.midterm.find({gpa:{$eq:5.5}}, {name:1})

>db.midterm.find({gpa:{$lt:5}}).pretty()

Other operators

in for in the list

nin for not in the list

For example,

>db.midterm.find({gpa:{$nin:[2.5,7.5]}} )

>db.midterm.find({gpa:{$nin:[2.3,7.8]}} )

4. Delete Operation:

A document can be removed from collection by the following command:

> db.midterm.find({name:”Rohit”})

To drop a document from the collection run the following command:

>db.midterm.remove({_id=4})

To drop a collection, you can use the following command:

>db.temp.drop()

Conclusion:

In this article I have explored the basic operations Create, Update, Read and Delete (CURD) in MongoDB. It is supper simple to perform these operations on NOSQL document database MongoDB. I am sure, it can be a stepping point for the novice MongoDB users. It is widely used nowadays.

On wrapping up notes, thank for the read and feel free to share your comments. Your comments will surely help me to present contents in a better way. See you next week.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Virendra Kumar Shrivastava

Dr. Virendra Kumar Shrivastava

Professor || Alliance College of Engineering and Design || Alliance University || Writer || Big Data Analytics