Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

MongoDB

4 min readJun 3, 2022

--

Press enter or click to view image in full size
MongoDB

In this article, we will discuss about NoSQL, the basics of mongoDB and how to do CRUD operations in mongoDB.MongoDB

What is NoSQL?

NoSQL is also known as “not only SQL”. They are non-tabular databases. They store data in a different manner than SQL databases. These kinds of databases come in various types based on their data model, such as document, key-value, wide-column, and graph. They are more flexible, scalable, distributed databases. Most of the NoSQL databases are open source.

There are mainly four types of NoSQL database systems that exist. Each of them uses different types of data models.

  • Document databases
  • Graph databases
  • Key-value stores
  • Wide-column stores

Advantages of NoSQL

  • The workload for interactive real-time web applications is simplified.
  • NoSQL caters to flexibility for data that is not normalized and requires flexible data models.
  • They provide great scalability for large data sets.
  • They are greatly suited for cloud applications, mobile, and big data requirements.

Disadvantages of NoSQL

  • NoSQL has its own querying languages. Which is in contrast to SOL.
  • As NoSQL databases are newer, there are no standards as relational databases.
  • There is no such schema as relational databases have.

What is MongoDB?

MongoDB is a No SQL database. It is an open-source, cross-platform, document-oriented database written in C++.

MongoDB was developed by a NewYork based organization named 10gen which is now known as MongoDB Inc. It was initially developed as a PAAS (Platform as a Service). Later in 2009, it is introduced in the market as an open source database server that was maintained and supported by MongoDB Inc.

Instead of storing data in tables like SQL, each record in MongoDB is stored in collections. There is a slight terminology difference between the relational database and MongoDB, they are as follows.

Press enter or click to view image in full size

Here’s a sample Document from a MongoDB collection.

Press enter or click to view image in full size

The primary purpose of building MongoDB

  • Scalability
  • Performance
  • High Availability
  • Provides high performance.
  • Scaling from single server deployments to large, complex multi-site architectures.
  • Key points of MongoDB
  • Develop Faster
  • Deploy Easier

Features of MongoDB

  1. Support ad hoc queries
  2. Indexing
  3. Replication
  4. Duplication of data
  5. Load balancing
  6. Database Triggers

MongoDB Advantages

  • MongoDB is schema less. It is a document database in which one collection holds different documents.
  • There may be difference between number of fields, content and size of the document from one to other.
  • Structure of a single object is clear in MongoDB.
  • There are no complex joins in MongoDB.
  • MongoDB provides the facility of deep query because it supports a powerful dynamic query on documents.
  • It is very easy to scale.
  • It uses internal memory for storing working sets and this is the reason of its fast access.

MongoDB Datatypes

Press enter or click to view image in full size

Let’s move to the CRUD functions in mongoDB…

Get Gowsigan Pushpanathan’s stories in your inbox

Join Medium for free to get updates from this writer.

Create Operation

There are two ways to insert data to the MongoDB. We’ll use the RecordsDB collection in this example.

  1. insertOne() => To add single data to the collection

2. insertMany() => To add multiple data to the collection.

Read Operation

As in Create there are two ways to retrieve data from the MongoDB.

  1. find() =>To get all data from the collection.

db.RecordsDB.find()

Press enter or click to view image in full size
Output

2. findone() => To get single data that satisfies search criteria.

syntax : db.{collection}.findOne({query}, {projection})

when we run this code we will get the below output. db.laliga.find({“age”:34})

Press enter or click to view image in full size
Output

Update Operation

There are 3 ways to update data in MongoDB.

  1. updateOne() => Updating existing single data
Output

2. updateMany() => Updating existing multiple data

Press enter or click to view image in full size
Output

3. replaceOne() => Replace a single data from MongoDB

Press enter or click to view image in full size
output

Delete Operation

There are two ways to delete data from the MongoDB.

  1. deleteOne() => used to remove single data

db.laliga.deleteOne({name:”Kylian Mbappe”})

output

2. deleteMany() => used to remove multiple data

db.RecordsDB.deleteMany({team:”FC Barcelona”})

output

Conclusion

By using this article, you learned NoSQL and the notation of CRUD operations using mongoDB and the characteristics of mongoDB.

Thank you so much for reading my article and I hope you have a better idea from this article.

Thank you.

--

--

Gowsigan Pushpanathan
Gowsigan Pushpanathan

Written by Gowsigan Pushpanathan

Hi! I’m Gowsigan. I’m reading full-stack Web Development at uki

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development