MongoDB vs SQL

In detail explanation about NoSql structure. Concept is explained in detail, quoting different examples.

Danish Siddiq
tajawal
4 min readSep 19, 2019

--

This series will focus on understanding NoSQL fundamentals and writing queries from the beginner to the advanced level using MongoDB. If you have a concept of NoSQL then jump to the part-II of this series to practice designing a database in the NoSQL.

This article is written considering the features of MongoDB version 4.0. Following areas are covered in detail:

  1. What is MongoDB?
  2. MongoDB vs SQL structure
  3. Benefits
  4. Storage features
  5. Few of the frequently asked questions

1. What is MongoDB?

MongoDB falls into the category of NoSQL, where data is NOT stored in a conventional table or row format. In MongoDB data is stored in collections that are analogous to SQL tables.

2. MongoDB vs SQL structure:

MongoDB stores documents in the collection in BSON. The document behaves in the same way as rows in tables. Documents have data in JSON format where fields are analogous to columns in SQL.

For illustration purpose, let's take an example of student table from SQL structure, and transform into the MongoDB structure.

Data from the Student table transformed into the Student collection

No complexity of setting up table details. Nested object details are easy to nest, like “address” in the above example. In the future, if there will be a requirement to add one more line in the “address” object then simply add fields in “address”, while in a table format, a whole new column is required.

3. Benefits:

  1. Amazing response time with huge size of data
  2. Scalability vertically and horizontally, few applications are running more than 100 nodes of MongoDB with quick performance
  3. Easy maintenance, high availability for replication sets. Sharding is supported where horizontal scaling is possible by splitting data across multiple instances.
  4. MongoDB does not require SQL normalization restrictions. When data grows in SQL, referential key constraints and joins recede the performance, whereas MongoDB performance is not dependent on such factors.

4. Storage features:

There are two approaches to store data:

A. WiredTiger

B. InMemory

A. WiredTiger:

MongoDB by default supports this mode, where data is stored on disk. Few of the key features:

  1. Document-level concurrency, multiple document editing is possible in a collection. Dropping collection and few other operations involving multiple databases requires a database lock
  2. WiredTiger uses both WiredTiger internal cache and file system cache to enhance performance. The default WiredTiger cache size is 50% of (RAM-1 GB) or 256 MB. Lesser I/O operations, so better performance.
  3. If two operations conflict then MongoDB will retry the failed operation transparently in the backend
  4. Compression is supported to save storage size at the cost of CPU utilization

B. InMemory:

It is a part of general availability, where InMemory does not store data on a disk other than some metadata and diagnostics. Keys features:

  1. Avoiding Disk I/O increased performance amazingly
  2. Document-level concurrency is supported to allow multiple operations on a collection.

5. Frequently asked questions:

A. How to define the schema for collection?

No need to define a schema for a collection, documents may have fields which are not present in other documents. Although it is a good practice to have homogenous schema but not mandatory.

Data types for fields can differ across documents in a collection. Whereas from version 3.2, you can enforce validation while updating or inserting a document.

B. Does MongoDB support SQL?

No, SQL queries are not supported. MongoDB has its own syntax for writing queries calls MongoDB query language. Part-IV of the series illustrates writing beginner level MongoDB queries.

C. How to create a database and collection in MongoDB?

If the database does not exist in MongoDB, it creates a database when data is stored for the collection. For example:

use student_enrollmentdb.student.insertOne({ firstName: "Munir", lastName: "Niazi" });

If the database with name “student_enrollment” does not exist then the above statement will create a database with a collection name“student” containing newly inserted record else it will do an addition in the existing collection.

D. Can you mix storage engines for better performance?

Yes, it is possible but usually, most applications use WiredTiger.

E. Does MongoDB handle caching?

Yes, MongoDB keeps recently used data in RAM. If a query contains indexes and data set fits into RAM then all data will be served from RAM

F. Does MongoDB support indexing?

Yes, indexes are supported.

Part II:

It contains modeling the NoSQL database in detail with multiple examples having 1:M and M:M relationships.

References:

https://www.mongodb.com

--

--