A must-know technology for any full-stack developer ; MongoDB

Kevinwijesooriya
4 min readMar 17, 2022

What is MongoDB?

MongoDB is a free and open source NoSQL database management system. NoSQL is a database technology that is used as an alternative to traditional relational databases. NoSQL databases are extremely useful when dealing with large amounts of distributed data. MongoDB is a tool for managing document-oriented data, as well as storing and retrieving data.

How it works?

MongoDB employs records, which are made up of documents that contain a data structure made up of field and value pairs. In MongoDB, the basic unit of data is the document. The documents resemble JavaScript Object Notation, but they use a variant known as Binary JSON (BSON). The advantage of using BSON is that it supports more data types. These documents’ fields are analogous to the columns in a relational database. According to the MongoDB user manual, the values contained can be a variety of data types, including other documents, arrays, and arrays of documents. As a unique identifier, documents will also include a primary key.

Collections

Collections are groups of documents that serve as the equivalent of relational database tables. Collections can contain any type of data, but the data in a collection cannot be spread across multiple databases.

The mongo shell

The mongo shell is a standard component of MongoDB’s open source distributions. Users connect the mongo shell to their running MongoDB instances after installing MongoDB. The mongo shell is an interactive JavaScript interface to MongoDB that allows users to query and update data as well as perform administrative tasks.

Why should you use MongoDB?

MongoDB provides high performance data persistence

MongoDB supports a rich query language to support read and write operations (CRUD)

High Availability

MongoDB provides horizontal scalability as part of its core functionality

Support for Multiple Storage Engines

MongoDB vs NoSQL

MongoDB and SQL databases are two polar opposites in the backend world. The former works with chaotic unstructured data, whereas the latter works with organized structured data.

SQL databases, also known as relational databases, were created to store data with a structured schema. The schema represents the database design to which the data should adhere. Data in a structured schema is saved in a row-column format known as a Table and can be retrieved using queries formatted in the Structured Query Language (SQL).

In MySQL, data is stored in tables, where the column represents an attribute and the row represents a specific record. These tables, in turn, are stored in databases. Data in MongoDB is stored in collections, which are similar to MySQL tables. A collection can contain a large number of documents in which data is stored in key-value JSON format. Within a MongoDB database, there may be hundreds of such collections.

Documents

Data records in MongoDB are stored as BSON documents. The binary representation of JSON documents is known as BSON.

Document Structure

MongoDB documents are made up of field-and-value pairs and have the structure shown below.

{
field1: value1,
field2: value2,
field3: value3,
...
fieldN: valueN
}

A field’s value can be any of the BSON data types, such as other documents, arrays, and arrays of documents.

var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"),
name: { first: "Alan", last: "Turing" },
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}

MongoDB CRUD Operations

Create Operations

Read Operations

Update Operations

Delete Operations

Document Structure

The structure of documents and how the application represents data relationships are the two most important decisions in designing data models for MongoDB applications. Related data can be embedded within a single document in MongoDB.

Embedded Data

By storing related data in a single document structure, embedded documents capture data relationships.

References

References are used to store data relationships by including links or references from one document to another.

That concludes this article’s content; see you in the next one.

You can access the documentation via the link provided below.

https://docs.mongodb.com/manual/tutorial/getting-started/

--

--