MongoDB
- Pronunciation
- MONG-goh-dee-bee
In short
MongoDB is a document database that stores data as flexible JSON-like documents instead of table rows, so records in one collection can have different fields.
What is MongoDB?
MongoDB is a NoSQL document database first released in 2009 by the company now called MongoDB, Inc. Instead of tables, rows and columns, it stores documents in collections. A document is a JSON-like object, saved internally in a binary format called BSON, that can contain nested objects and arrays, so one document can hold a user together with their addresses and preferences.
Because there is no fixed schema, documents in the same collection can have different fields, and new fields can be added without a migration. Data that is read together is often stored together in one document, which avoids joins and makes typical reads fast. Indexes, an aggregation pipeline for grouping and transforming data, and multi-document ACID transactions are all built in.
MongoDB scales out across machines with sharding, which splits a collection by a shard key, and it keeps copies of data on several servers in a replica set, so a new primary is elected if one fails. MongoDB Atlas, the company's managed cloud service, is a common way to run it.
A common misconception is that a flexible schema means no design is needed. Without validation rules, data drifts into inconsistent shapes, and relationships between many collections can be awkward to query. MongoDB fits data that is naturally document-shaped; strongly relational data with many links between records often fits a relational database better.
Key takeaways
- MongoDB stores JSON-like documents (BSON) in collections, not rows in tables.
- Documents in one collection can have different fields.
- Data read together is usually stored together, avoiding joins.
- It scales out with sharding and stays available with replica sets.
- Flexible schemas still need validation and careful design.
Example
import { MongoClient } from "mongodb";
const client = new MongoClient("mongodb://localhost:27017");
const users = client.db("shop").collection("users");
// A document can nest objects and arrays
await users.insertOne({
name: "Ada",
email: "ada@example.com",
addresses: [{ city: "Istanbul", zip: "34000" }],
});
const ada = await users.findOne({ email: "ada@example.com" });Readers ask
Is MongoDB a relational database?
No. MongoDB is a NoSQL document database: it stores documents in collections instead of rows in tables and does not require a fixed schema.
Does MongoDB support transactions?
Yes. Single-document writes have always been atomic, and since version 4.0 MongoDB also supports multi-document ACID transactions.
Is MongoDB free?
The Community Server is free to use under the Server Side Public License (SSPL). The company also sells an enterprise edition and the managed Atlas service, which has a free tier.
Often compared
See also
- Document DatabaseDatabases, p. 15A document database is a NoSQL database that stores each record as a self-contained document, usually JSON-like, whose fields can differ from record to record.
- NoSQLDatabases, p. 29NoSQL is a family of databases that store data in models other than relational tables, such as documents, key-value pairs, wide columns, or graphs.
- JSONBackend & APIs, p. 25JSON is a lightweight, text-based format for storing and exchanging structured data as key-value pairs and lists, readable by both humans and machines.
- ShardingDatabases, p. 39Sharding is a way of scaling a database by splitting its data across several servers, called shards, so each one stores and handles only part of the total.
- Database ReplicationDatabases, p. 10Database replication is the continuous copying of data from one database server to others, so several servers hold the same data for reliability and scale.
- Database SchemaDatabases, p. 11A database schema is the blueprint of a database that defines its tables, columns, data types, relationships, and the rules that stored data must follow.
Spotted a mistake or something missing on this page?Suggest an edit