banner18

Introduction to MongoDB

 

 

What is MongoDB?

MongoDB is a NO SQL database which is highly scalable,flexible, memory efficient  and easy to use. Unlike relational databases, it does not use tables to store data. It uses documents and collections to store data. One might wonder how is that useful, it allows each entry in a collection to have a different number of columns with different names so in this way it is more flexible and treats each entry independently . 

This architecture allows Mongodb to be more memory efficient . Each database in MongoDb has a set of collections which contain documents which can have it’s own set of fields independent of the collection.

Each document can have a nested document which helps to address the relationship between the fields better.

 

MongoDB vs SQL  

Let’s compare MongoDb with MySQL to get a better idea. 

 

Basic operations in  MongoDB

  • INSERT

Insert operations in mongodb come in two flavours one to insert a single document (insertOne()) and other to insert more than one entries(insert() or insertMany()).

Note that if a collection does not exist an insert operation will automatically create one.

 

insertOne()-

It is used to insert a single entry in the database.

Syntax

db.Collection_name.insertOne()

 

insert()-

It can be used to insert a single or multiple documents in the collection

Syntax-

db.Collection_name.insert([,....])

 

  • SEARCH FOR A RECORD/ DISPLAY 

We use a find command  to display records from a collection. It also gives an option to display selected fields from the selected document.

 

find()-

Syntax-

db.Collection_name.find(query, projection)

 

Query- it can be a valid mongodb query like 

            {field1:value, field:value2...}

Projection- it decides whether a particular will be displayed(1) or not(0)

                  Eg. {field1:1 or 0, field2: 1or 0}

 

  • DELETE 

We can delete a single record from the collection by using deleteOne() and multiple records by using deleteMany()

deleteOne(filter)-

It only deletes one record from the collection. If multiple records satisfy the condition(filter) the first one is deleted.

Syntax-

db.Collection_name.deleteOne({field1:value1, field2:value2...})

 

deleteMany(filter)-

Deletes all the documents which match the filter .

Syntax-

db.Collection_name.deleteMany({field1:value1, field2:value2...})

 

  • UPDATE 

Just like the delete operation , update operation in MongoDB is of two types: updateOne() and updateMany() .

 

updateOne()-

It updates a single document from the collection based on the filters.

Syntax:

db.Collection_name.updateOne(,,)

 

Filter- It is a document used to select the criteria for update

          {field1: value1, field2:value2}

Update: It can be a document or a pipeline to set value or add      fields

 

updateMany()-

It updates all the documents which match the filter.

         Syntax:

         db.Collection_name.updateMany(,)

 

         Filter- It is a document used to select the criteria for update

          {field1: value1, field2:value2}

Update: It can be a document or a pipeline to set value or add      fields

         

CONCLUSION

In this article we discussed MongoDB, how it is different from SQLand the advantages it has over SQL like scalability. We also learnt how to perform basic operations in MongoDB like insert , delete , update and display. 

  •  March, 26, 2021
  • Yash Burad
POST A COMMENT
We'll never share your email with anyone else.
Save my name, email, and website in this browser for the next time I comment.
Latest Blogs