Skip to main content

Document Database

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/document-database

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

Storing and querying nested documents (MongoDB syntax)javascript
// 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings