Document Database
- In Turkish
- Doküman Veritabanı
In short
A 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.
What is a document database?
A document database stores data as documents, usually in JSON or a binary form of it, and groups similar documents into collections. Each document holds everything about one item, including nested objects and arrays. A blog post document, for example, can contain its title, its tags, and its author's name in one place.
Every document has a unique ID, and the database can index and query any field inside it, including nested ones such as address.city. The schema is flexible: two documents in the same collection may have different fields, although most document databases let you add validation rules when you want them. Changes to a single document are atomic, many products now also support multi-document transactions, and collections can be sharded across servers to scale out. MongoDB and CouchDB are well-known examples, and PostgreSQL's JSONB type offers document-style storage inside a relational database.
Think of a folder of paper forms where each form is complete on its own and some forms have extra sections filled in. That model fits data that is usually read and written as a whole, such as product catalogs, content management systems, user profiles, game state, and mobile app backends.
The most common confusion is with a relational database. A relational design splits an order across orders and order_items tables and joins them, while a document database can embed the items inside the order document, which makes reading the whole order fast but makes reports across many documents harder and duplicates data that must be kept in sync. A document database also differs from a key-value store, which treats each value as an opaque blob fetched only by key; a document database understands the fields inside the value and can search and index them.
Key takeaways
- Each record is a self-contained, JSON-like document stored in a collection.
- Documents can nest objects and arrays, and their fields can vary.
- Any field, including nested ones, can be indexed and queried.
- Embedding related data speeds up reads but duplicates information.
- It is one of the most common kinds of NoSQL database.
Example
// One document holds the order and its line items together
await db.orders.insertOne({
customer: { name: "Ada", city: "London" },
items: [
{ sku: "BOOK-1", qty: 2, price: 12.5 },
{ sku: "PEN-3", qty: 1, price: 3 },
],
status: "paid",
});
// Query and index fields nested inside documents
await db.orders.createIndex({ "customer.city": 1 });
const london = await db.orders.find({ "customer.city": "London" }).toArray();Readers ask
Is a document database the same as NoSQL?
A document database is one type of NoSQL database. NoSQL also includes key-value stores, wide-column stores, and graph databases.
Should I embed related data or reference it?
Embed data that is read together and belongs to one parent, such as the line items of an order. Reference data by ID when it is shared by many documents, changes often, or can grow without limit, such as the comments on a popular post.
Does a document database have a schema?
It does not require one up front, which is why it is called schemaless or schema-flexible. In practice the application still expects certain fields, and most document databases can enforce optional validation rules.
See also
- 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.
- Relational DatabaseDatabases, p. 38A relational database stores data in tables of rows and columns, links those tables through keys, and lets you query and combine the data with SQL.
- Key-Value StoreDatabases, p. 25A key-value store is a NoSQL database that saves each piece of data under a unique key, so an application can read or write it by that key very quickly.
- DenormalizationDatabases, p. 14Denormalization is the deliberate duplication of data across tables or documents so that frequent reads need fewer joins, at the cost of more complex writes.
- 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.
- MongoDBDatabases, p. 26MongoDB 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.
- FirebaseDatabases, p. 20Firebase is Google's platform that gives web and mobile apps a hosted database, authentication, storage, hosting and functions without managing a backend.
Spotted a mistake or something missing on this page?Suggest an edit