Search⌘ K
AI Features

NoSQL API Documents

Explore how Cosmos DB stores data as flexible JSON documents instead of traditional tables, allowing schema-free design. Understand challenges like data consistency, document versioning, and TTL management. Learn strategies for modeling related entities and using discriminator fields to optimize queries within partitions.

The elephant in the room

Cosmos DB doesn’t use conventional tables to store the data. For the database, everything is a JSON document!

A document representing a game for our store might look like the following:

Elixir
{
"title": "Mario",
"year": 1983
"category": "Platform",
"price": 20
}

But why would anyone prefer JSON over a predefined column-based structure?

No rules

The first thing that comes to mind when thinking about documents is flexibility. Usually, in relational databases, we first need to define a set of columns for our table.

Each column usually has the following:

  • Name

  • Type (string, integer, boolean)

  • A column value if it’s required, otherwise, it can be left empty

  • Sometimes a default value, if no column value is present

Every time we want to change the columns, we need to alter the table, and the change affects all existing rows. Sometimes, based on constraints, the database blocks the operation altogether. With JSON documents, we ...