Topic 4 of 4
Cosmos DB doesn't lock you into a single data model or query language. It offers several APIs compatible with tools developers already know. The exam gives a scenario (an app on MongoDB, a graph, a key-value table) and asks for the right API.
Contents
When you create a Cosmos DB account, you choose the API. That choice determines how the application interacts: what format it sends, what query language it uses, and which client libraries it works with. Internally, Cosmos DB stores data in its own format; the API is an abstraction layer on top.
The benefit: portability
If a team already has an application on MongoDB or Apache Cassandra, it can point it at Cosmos DB with minimal changes and gain global distribution, managed throughput, and SLA-backed availability.
The five supported APIs are NoSQL, MongoDB, Table, Apache Cassandra, and Apache Gremlin. Azure Cosmos DB for PostgreSQL is a separate offering on a retirement path, not recommended for new projects.
It's Cosmos DB's native API. It stores data as JSON documents and lets you query it with SQL-like syntax. It was previously called the SQL API: if older documentation says "SQL API", it's the same service.
SELECT *
FROM customers c
WHERE c.id = "joe@litware.com"
-- Result: a JSON document
{
"id": "joe@litware.com",
"name": "Joe Jones",
"address": { "street": "1 Main St.", "city": "Seattle" }
}Compatible with MongoDB drivers and client libraries, so existing applications connect without significant changes. Data is stored in BSON (binary JSON) and queried with MQL (MongoDB Query Language), an object-oriented syntax where you call methods on collections.
db.products.find({id: 123})
-- Result
{ "id": 123, "name": "Hammer", "price": 2.99 }An important nuance
It fits when the team knows MongoDB and wants to use its drivers with Cosmos DB's global model. To migrate an existing MongoDB application or use the full MQL, Microsoft recommends Azure DocumentDB, a separate service with MongoDB wire protocol compatibility.
It stores key-value pairs in tables with the same programming model as Azure Table Storage. A Table Storage app connects with minimal changes and gains scalability, global distribution, and automatic indexing on every property.
| PartitionKey | RowKey | Name | |
|---|---|---|---|
| 1 | 123 | Joe Jones | joe@litware.com |
| 1 | 124 | Samir Nadoy | samir@northwind.com |
Each row is identified by PartitionKey + RowKey and retrieved through a REST-style endpoint, for example Customers(PartitionKey='1',RowKey='124').
Compatible with Apache Cassandra, an open-source database with a column-family storage model: rows don't have to contain the same columns, unlike a relational table.
SELECT * FROM Employees WHERE ID = 2It uses CQL (Cassandra Query Language), with SQL-like syntax. It fits when migrating a Cassandra workload to a managed cloud database.
Designed for graph data: entities are vertices (nodes) and relationships are edges. Useful when the connections matter as much as the data: social networks, recommendation engines, fraud detection, and organizational hierarchies.
g.addV('employee').property('id', '3').property('firstName', 'Alice')
g.V('3').addE('reports to').to(g.V('1'))
g.V().hasLabel('employee').order().by('id')Gremlin is the language for traversing and manipulating the graph: the example adds an employee, connects her to her manager, and then lists employees in order.
| API | Data model | Language | Choose it if… |
|---|---|---|---|
| NoSQL | JSON documents | SQL-like syntax | A new app, maximum scale |
| MongoDB | BSON documents | MQL | The team uses MongoDB |
| Table | Key-value | Table Storage API | You're coming from Azure Table Storage |
| Cassandra | Column family | CQL | You're coming from Apache Cassandra |
| Gremlin | Graph | Gremlin | The relationships are what matters |
| Situation | API | Why |
|---|---|---|
| A new app with JSON documents and SQL-like query syntax | API for NoSQL | The native Cosmos DB API. |
| Analyze operational data in near real time without building ETL | API for NoSQL with Fabric mirroring | Replicates to Fabric without consuming RUs. |
| An existing application using MongoDB drivers | API for MongoDB | Compatible with MongoDB drivers and libraries. |
| Migrate an Azure Table Storage app while gaining global distribution | API for Table | The same programming model. |
| Migrate an Apache Cassandra workload to a managed service | API for Apache Cassandra | Column families and CQL. |
| Detect fraud by traversing relationships between accounts | API for Apache Gremlin | Graphs with vertices and edges. |
| Migrate MongoDB using the full MongoDB Query Language | Azure DocumentDB | Microsoft's recommended option for that case. |
❓ Is Cosmos DB's "SQL API" a different API from NoSQL?
No. It's the former name of the API for NoSQL.
❓ Does the API for NoSQL turn Cosmos DB into a relational database?
No. It stores JSON documents; only the query syntax resembles SQL.
❓ What format does the API for MongoDB store data in?
BSON (binary JSON), queried with MQL.
❓ Which language does the API for Apache Cassandra use?
CQL (Cassandra Query Language).
❓ What are entities and relationships called in Gremlin?
Vertices (nodes) and edges.
❓ How many APIs does Cosmos DB support?
Five: NoSQL, MongoDB, Table, Apache Cassandra, and Apache Gremlin.
❓ Is the API chosen per container?
No. It's chosen when you create the Cosmos DB account.
Did you get this topic?
Test what you just learned
A social network wants to store users and who follows whom, and quickly query a person's friends of friends. Which Cosmos DB API should it use?
Original questions, each with a full explanation.
Start a mock examSign in to save your progress.