TExcelTAD
Home › MongoDB › MongoDB Data API

MongoDB Data API

Drivers talk to MongoDB over its binary wire protocol, on a long-lived TCP connection. That is ideal for a backend server, but impossible from places that can only make HTTPS requests: a browser, a spreadsheet script, an IoT device or a serverless function without a driver. A data API fills that gap: an HTTPS endpoint that accepts a query as JSON and returns documents as JSON.

The Atlas Data API (retired)

MongoDB Atlas used to offer exactly this as the Atlas Data API: endpoints such as /action/findOne and /action/insertOne secured with an API key. A request looked like this:

bash
curl -X POST "https://data.mongodb-api.com/app/<app-id>/endpoint/data/v1/action/find" \
  -H "Content-Type: application/json" \
  -H "api-key: <API key>" \
  -d '{
        "dataSource": "Cluster0",
        "database": "shop",
        "collection": "products",
        "filter": { "category": "tea" },
        "projection": { "name": 1, "price": 1 },
        "limit": 5
      }'

and the response was { "documents": [ … ] }. MongoDB deprecated the Data API (together with Atlas Device Sync and HTTPS endpoints) in 2024 and shut it down in September 2025. The idea lives on, though, and you build it yourself today:

Option How it works
Your own REST API a small Express/NestJS, FastAPI, Spring or ASP.NET service using the official driver
Serverless functions AWS Lambda, Vercel or Cloudflare functions that use a driver and expose HTTP endpoints
Third-party gateways community and commercial projects that recreate the Data API's endpoints
GraphQL a GraphQL server on top of the driver

Whatever you choose, the API translates JSON requests into the same Query API you have learned: filter, projection, sort, limit, update documents and pipelines.

Extended JSON: types over HTTP

Plain JSON has no dates, ObjectIds, 64-bit integers or decimals. Data APIs therefore send Extended JSON (EJSON), which wraps such values in objects like { "$date": … } and { "$oid": … }. mongosh has an EJSON helper to convert in both directions:

mongosh · try it yourself
const doc = db.customers.findOne({ _id: 1 }, { name: 1, joined: 1 });
print(EJSON.stringify(doc));
print(EJSON.stringify({ id: ObjectId('6650a1b2c3d4e5f601234567'), qty: NumberLong('5'), price: NumberDecimal('9.99') }));
{"_id":1,"name":"Ada Lovelace","joined":{"$date":"2026-01-04T09:15:00Z"}}
{"id":{"$oid":"6650a1b2c3d4e5f601234567"},"qty":5,"price":{"$numberDecimal":"9.99"}}

There are two flavours. Relaxed mode (the default) writes numbers as plain JSON numbers where possible; canonical mode keeps every type exactly:

mongosh · try it yourself
const value = { count: NumberInt(3), ratio: 0.5, when: ISODate('2026-06-01T12:00:00Z') };
print('relaxed:  ', EJSON.stringify(value));
print('canonical:', EJSON.stringify(value, { relaxed: false }));
relaxed:   {"count":3,"ratio":0.5,"when":{"$date":"2026-06-01T12:00:00Z"}}
canonical: {"count":{"$numberInt":"3"},"ratio":{"$numberDouble":"0.5"},"when":{"$date":{"$numberLong":"1780315200000"}}}

Parsing a JSON request

On the server side, an API receives EJSON text and turns it back into BSON values before querying. A filter that arrived as text, with a date inside:

mongosh · try it yourself
const body = '{"filter": {"date": {"$gte": {"$date": "2026-05-20T00:00:00Z"}}}, "limit": 3}';
const request = EJSON.parse(body);
print(request.filter.date.$gte instanceof Date);
const docs = db.orders.find(request.filter, { status: 1, total: 1 }).sort({ _id: 1 }).limit(request.limit).toArray();
print(EJSON.stringify({ documents: docs }));
true
{"documents":[{"_id":1017,"status":"cancelled","total":30},{"_id":1018,"status":"processing","total":83},{"_id":1019,"status":"processing","total":11.5}]}

Warning: Never pass a filter from an untrusted client straight to the database. A public API should accept specific parameters (a category, a page number) and build the query itself, or validate the filter strictly. Otherwise users can read data they shouldn't, or send expensive queries such as unindexed $regex scans.

Test yourself

Why would an application use an HTTPS data API instead of a driver?

  1. It is always faster
  2. The client can only make HTTP requests (browser, serverless, IoT)
  3. Drivers don't support queries
  4. To avoid indexes

How does Extended JSON represent a date?

  1. As a plain string
  2. As { "$date": … }
  3. As a Unix timestamp number
  4. Dates can't be sent