Skip to content

MongoDB - Overview

MongoDB is a cross-platform, document-oriented NoSQL database. It is designed for high performance, high availability, and easy scalability. Instead of storing data in tables and rows as in relational databases, MongoDB stores data in flexible, JSON-like documents.

A database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server can host multiple databases.

A collection is a group of MongoDB documents, roughly equivalent to a table in a relational database (RDBMS). A collection exists within a single database. Collections do not enforce a schema by default, meaning documents within a collection can have different fields.

A document is a set of key-value pairs, stored in a format called BSON (Binary JSON). BSON extends JSON with additional data types, such as ObjectId, Date, and Int64, for optimized storage and speed. Documents have a dynamic schema, allowing for rich, hierarchical data structures.

The following table shows the relationship of RDBMS terminology with MongoDB.

RDBMSMongoDB
DatabaseDatabase
TableCollection
RowDocument
ColumnField
Table JoinEmbedded Documents / Lookups ($lookup)
Primary KeyPrimary Key (Default key _id provided by MongoDB)
Servermongod (The database process)
Clientmongosh (The MongoDB Shell)

The following example shows the document structure for a blog post, using BSON data types.

{
"_id": ObjectId("64f8a00e23e8b1e1ab123456"),
"title": "Understanding Modern MongoDB",
"content": "MongoDB is a powerful NoSQL database...",
"author_username": "tech_writer_jane",
"url": "/blog/modern-mongodb",
"tags": ["mongodb", "database", "nosql", "webdev"],
"published_at": ISODate("2023-09-06T10:00:00Z"),
"stats": {
"likes": 150,
"views": 4500
},
"comments": [
{
"user": "reader_dave",
"text": "Great article! Very helpful.",
"posted_at": ISODate("2023-09-06T11:30:00Z"),
"likes": 12
},
{
"user": "dev_sara",
"text": "Could you add more on indexing?",
"posted_at": ISODate("2023-09-07T14:22:00Z"),
"likes": 5
}
]
}

The _id field is a 12-byte BSON type that guarantees document uniqueness. It is automatically generated by MongoDB if not provided. The 12 bytes are composed of: a 4-byte timestamp, a 5-byte random value, and a 3-byte incrementing counter.

While you can run MongoDB on your own servers, most modern applications leverage cloud-based Database-as-a-Service (DBaaS) solutions. MongoDB Atlas is the official managed database service, handling infrastructure, scaling, backups, and security, allowing developers to focus on building applications.