MongoDB Data API

The MongoDB Atlas Data API

The Data API is a game-changing feature in Atlas. It allows you to read and write data over HTTPS. You do not need a traditional database driver.

Why Use the Data API?

It is built for modern, serverless environments. Serverless functions suffer from connection limits. The HTTP Data API bypasses strict connection pooling limits.

How It Connects

Standard drivers maintain open TCP sockets. The Data API uses standard stateless web requests. It uses simple POST endpoints provided by Atlas.

Authentication

You authenticate using a secure API Key. You pass this key in the request headers. It is completely safe when used over HTTPS.

Querying with JSON

The payload you send is pure JSON. You include the database, collection, and filter rules. Atlas processes it and responds with JSON data.

Data API Request Example:

// Fetch data using standard fetch()
fetch("https://data.mongodb-api.com/app/.../endpoint/data/v1/action/findOne", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "api-key": "YOUR_ATLAS_API_KEY"
  },
  body: JSON.stringify({
    "dataSource": "Cluster0",
    "database": "sample_mflix",
    "collection": "movies",
    "filter": { "year": 2015 }
  })
});

Supported Operations

The Data API supports standard CRUD methods. You can call findOne, find, insertOne, and updateOne. You can even execute complex aggregation pipelines.

Edge Computing

Edge networks like Cloudflare Workers love the Data API. They lack full TCP support for standard drivers. The Data API runs perfectly on the Edge.

Summary

The Atlas Data API opens up new integration methods. It uses standard HTTPS requests and JSON payloads. It is ideal for serverless apps and edge deployments.

Exercise

How does the Atlas Data API connect to the database?