Skip to main content

Introduction to NoSQL & MongoDB

What is NoSQL?

NoSQL (Not Only SQL) databases are non-tabular databases and store data differently than relational tables. NoSQL databases come in a variety of types based on their data model. The main types are document, key-value, wide-column, and graph.

SQL vs NoSQL

FeatureSQL (Relational)NoSQL (Non-Relational)
StructureTables with fixed rows and columnsDocument: JSON documents, Key-value: key-value pairs, etc.
SchemaRigidFlexible
ScalabilityVertical (scale up with a larger server)Horizontal (scale out across commodity servers)
JoinsComplex joins supportedJoins typically handled in code or not supported

What is MongoDB?

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.

Key Features

  1. Document Model: Data is stored in documents (BSON format) grouped into collections. This maps naturally to objects in application code.
  2. Flexible Schema: Documents in the same collection do not need to have the same set of fields or structure.
  3. High Availability: Replica sets provide automatic failover and data redundancy.
  4. Horizontal Scalability: Sharding distributes data across a cluster of machines.
  5. Rich Queries: Supports ad-hoc queries, indexing, and real-time aggregation.

JSON vs BSON

MongoDB stores data in BSON (Binary JSON). BSON extends the JSON model to provide additional data types, ordered fields, and to be efficient for encoding and decoding within different languages.
  • JSON: Text-based, human-readable.
  • BSON: Binary-encoded, supports types like Date, ObjectId, and Binary data.

Summary

  • NoSQL databases offer flexibility and scalability compared to traditional SQL databases.
  • MongoDB is a document database that stores data in BSON format.
  • It is designed for modern application development with a flexible schema and powerful query capabilities.