What is MongoDB?

MongoDB is an Open Source and cross-platform NoSQL Document Database. It was developed by MongoDB Inc. MongoDB is the most popular NoSQL database out there. It is a widely used database.

MongoDB is a general-purpose document database. It structures the data into documents that are similar to JSON objects. JSON objects used in the MongoDB document are fundamentally different from the way relational database management system (RDBMS) tables structure data into rows and columns.

MongoDB provides drivers in all major programming languages, making it easier to connect to the MongoDB database from our applications, regardless of the language we use. As a general-purpose database, MongoDB has a wide variety of use cases. It is used in everything from small personal education projects to startups to large mission-critical enterprise applications.

Some common examples of use cases of MongoDB include e-commerce, content management, the internet of things (IoT), time series data, trading and payments, gaming, mobile apps, and real-time analytics and AI. Some of the characteristics that make MongoDB popular across all use cases are its scalability, resilience, speed of development, high levels of data privacy, and security.

1. Difference terminologies in MongoDB

Terminologies used in MongoDB differ from those in RDBMS. If you are already familiar with RDBMS, there are some terminologies to be familiar with before using MongoDB. See the RDBMS equivalent on MongoDB below:

RDBMS (MySQL, Oracle, etc)MongoDB
DatabaseDatabase
TableCollection
RowDocument
ColumnField
RDBMS and MongoDB terminologies

1.1. Database

As per the definition written in Wikipedia.

Wikipedia

A database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage

In simple terms, a database is a placeholder for all the data. Data can be any information such as your bank account information, personal information, test score sheets, etc.

MongoDB database:

  • Is an organized way to store and access data.
  • Is a NoSQL database. It uses documents to store data in an organized way.
  • It is a container for our collections.

1.2. Collection

A collection is an organized store of documents in the MongoDB database. It can have one or more documents. A collection is a group of documents.

A document is a structured way to organize and store data as a set of key-value (Fields-Values) pairs. A document is a basic unit of data in MongoDB.

1.4. Field

A field in a MongoDB document is a unique key to identifying its value. The equivalent of a Field in MongoDB is a Column in the Table of RDBMS.

Things to remember:

  • A field is a unique identifier for a specific datapoint.
  • Each field has a value associated with it.

2. MongoDB document

Everything is in rows and columns in RDBMS but here in MongoDB, we only have a document. The document can have multiple fields and values mapped to it. All the data in MongoDB documents are stored in key-value pairs. All the keys are called fields and all the fields are unique in a single document.

Documents offer a flexible and developer-friendly way to work with our data. The documents correspond to objects in the code. They match how we think in code, which makes them intuitive to work with. The document model allows us to plan more easily and quickly how our application data will match the data stored in our database. It can be used for modeling data from almost any shape or structure. The document can model everything from simple key-value pairs to text, geospatial indices, time series, graphs, and much more. Document flexibility means that we can use one format to both model and query data for any application. This means development is going to be a lot easier and we can be more productive.

2.1. Structure of a MongoDB document

{
  "_id": "507f191e810c19729de860ea",
  "name": "Coder Sathi",
  "department": "Engineering",
  "salary": 50000
}

2.1.1 Key points to know about MongoDB document

  • The _id field is unique and is equivalent to a primary key in RDBMS.
  • The fields name, department, and salary are equivalent to the column in RDBMS. The beauty of MongoDB is that we don’t have to pre-define the data type of the field. It automatically uses the type based on the value given.
  • All of MongoDB’s documents are displayed in JSON format and stored in BSON. Therefore, working with MongoDB will be much easier if you already understand how JSON documents are formatted.
  • We can have nested documents much like JSON as all the documents are stored in JSON format. See the example below:
{
  "_id": "507f191e810c19729de860ea",
  "name": "Coder Sathi",
  "department": "Engineering",
  "salary": 50000,
  "educations": [
    {
      "degree": "BSC",
      "completedYear": 2013
    },
    {
      "degree": "MSC",
      "completedYear": 2013,
      "GPA": 3.9
    }
  ]
}
  • We can see that the educations field has multiple documents and these documents have different fields. This is also a valid document.
  • A MongoDB database can have multiple collections and a collection can have multiple documents usually with common fields.
  • Unlike RDBMS, we can scale MongoDB servers Horizontally. This means we can split the data into multiple servers. See the difference between Vertical and Horizontal scaling.
  • This database supports most of the popular programming languages like; Java, Python, Node.js, etc.

3. Conclusion

In this post, we learned and get the basic idea of the MongoDB database. We also learned the different terminology used in MongoDB compared to RDBMS and some useful information about this database as well.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments