MongoDB

MongoDB is a document-oriented NoSQL database that can hold a lot of information. MongoDB, unlike typical relational databases, employs collections and documents instead of tables and rows. MongoDB’s core data unit is key-value pairs, which are used to create documents. In the same way that relational database tables include sets of documents and functions, collections do as well. MongoDB is a database that has been around since the mid-2000s.

NoSQL Databases

There are three types of databases.

  1. RDBMS (Relational Database Management System)
  2. OLAP (Online Analytical Processing)
  3. NoSQL

A NoSQL database includes simplicity of design, simpler horizontal scaling to clusters of machines, and finer control over availability. The data structures used by NoSQL databases are different from those used by default in relational databases which makes some operations faster in NoSQL. The suitability of a given NoSQL database depends on the problem it should solve. Data structures used by NoSQL databases are sometimes also viewed as more flexible than relational database tables.

The Benefits of NoSQL

(i) Flexible Data Model :

NoSQL databases are highly flexible as they can store and combine any type of data, both structured and unstructured, unlike relational databases that can store data in a structured way only.

(ii) Evolving Data Model :

NoSQL databases allow you to dynamically adjust the schema to meet changing requirements while guaranteeing that your application is not disrupted or shut down.

(iii) Elastic Scalability :

NoSQL databases have the ability to scale to accommodate any type of data increase while remaining cost-effective.

(iv) High Performance :

NoSQL databases are designed for high performance, as measured by throughput (a metric of overall performance) and latency (it is the delay between request and actual response).

(v) Open-source :

NoSQL databases are cost-effective to implement because they don’t require hefty license costs and can run on low-cost hardware.

Disadvantages of NoSQL databases

(i) Lack of Standardization :

No standard exists that defines the rules and functions of NoSQL databases. The design and query languages of NoSQL databases vary widely between different NoSQL products — much more widely than they do among traditional SQL databases.

(ii) Backup of Database :

In NoSQL databases, backups are a disadvantage. Although certain NoSQL databases, such as MongoDB, include backup features, these technologies are not yet mature enough to provide a complete data backup solution.

(iii) Consistency :

NoSQL puts scalability and performance first but when it comes to a consistency of the data NoSQL doesn’t take much consideration so it makes it a little insecure as compared to the relational database e.g., in NoSQL databases if you enter the same set of data again, it will take it without issuing any error whereas relational databases ensure that no duplicate rows get entry in databases.

Collection

A collection is a group of MongoDB documents. It’s similar to an RDBMS table. Within a single database, there is a collection. Collections do not enforce a schema. Documents in a collection can have several fields allocated to them. In most cases, all of the documents in a collection have the same or similar purposes.

Document

A document consists of a collection of key-value pairs. The schema of documents is dynamic. Documents in the same collection don’t have to have the same set of fields or structure, and common fields in a collection’s documents can contain different types of data.

Key Components of MongoDB Architecture

Below are a few of the common terms used in MongoDB

  1. _id — This is a field required in every MongoDB document. The _id field represents a unique value in the MongoDB document. The _id field is like the document’s primary key. If you create a new document without an _id field, MongoDB will automatically create the field. So for example, if we see the example of the above customer table, Mongo DB will add a 24 digit unique identifier to each document in the collection.
  2. Collection — This is a grouping of MongoDB documents. A collection is the equivalent of a table that is created in any other RDMS such as Oracle or MS SQL. A collection exists within a single database. As seen from the introduction collections don’t enforce any sort of structure.
  3. Cursor — This is a pointer to the result set of a query. Clients can iterate through a cursor to retrieve results.
  4. Database — This is a container for collections like in RDMS wherein it is a container for tables. Each database gets its own set of files on the file system. A MongoDB server can store multiple databases.
  5. Document — A record in a MongoDB collection is basically called a document. The document, in turn, will consist of field names and values.
  6. Field — A name-value pair in a document. A document has zero or more fields. Fields are analogous to columns in relational databases.
  7. The following diagram shows an example of Fields with Key-value pairs. So in the example below CustomerID and 11 is one of the key-value pair’s defined in the document.

MongoDB CRUD Operations

CRUD operations create, read, update, and delete documents.

Create Operations

Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.

MongoDB provides the following methods to insert documents into a collection:

  • db.collection.insertOne() Version 3.2
  • db.collection.insertMany() Version 3.2

In MongoDB, insert operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

Read Operations

Read operations retrieve documents from a collection; i.e. query a collection for documents. MongoDB provides the following methods to read documents from a collection:

  • db.collection.find()

You can specify query filters or criteria that identify the documents to return

Update Operations

Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents of a collection:

  • db.collection.updateOne() Version 3.2
  • db.collection.updateMany() Version 3.2
  • db.collection.replaceOne() Version 3.2

In MongoDB, update operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

You can specify criteria, or filters, that identify the documents to update. These filters use the same syntax as reading operations.

Delete Operations

Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:

  • db.collection.deleteOne() Version 3.2
  • db.collection.deleteMany() Version 3.2

In MongoDB, delete operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

You can specify criteria, or filters, that identify the documents to remove. These filters use the same syntax as reading operations.

--

--

--

Software Engineering undergraduate of Sri Lanka Institute of Information Technology (SLIIT)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Scala on AWS Lambda — Benchmarking Serialization and Deserialization

1*9a7fALYM4xCe9QQ3jYRk4A.png

Tune up an upgraded Red Hat CodeReady Studio

Integrating Firebase Cloud Firestore and Firebase Auth in Flutter applications

How to disable bold font in gnome-terminal in Ubuntu 20.04

Developing Firmware for Low Power Devices

A decentralised marketplace

Auto Generated FAQ with Python Dash, Text Analysis and Reddit API

The $MTS IDO is Being Moved

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
Priyankara Athapaththu

Priyankara Athapaththu

Software Engineering undergraduate of Sri Lanka Institute of Information Technology (SLIIT)

More from Medium

Local eCommerce Project

EdenLoop ELT Listing Events on DigiFinex

First Prototype User Test

Basic MongoDB Commands