MongoDb - MongoDB Data API
1. What is the MongoDB Data API?
The MongoDB Data API is a RESTful API provided by MongoDB Atlas that allows you to interact with your MongoDB cluster without writing server-side code.
You can insert, read, update, delete, and aggregate documents directly via HTTPS requests.
It’s very useful when:
-
You're building serverless applications.
-
You want to avoid setting up backend SDKs.
-
You need to expose MongoDB operations over a secure REST API.
2. How It Works
-
You create a MongoDB Atlas cluster.
-
Enable the Data API in the Atlas dashboard.
-
Generate an API key or App Service ID.
-
Use HTTPS requests to interact with your database.
3. Enabling the Data API
Step 1 — Go to MongoDB Atlas
-
Log in to https://cloud.mongodb.com
-
Open your project.
Step 2 — Enable Data API
-
Go to App Services → Data API.
-
Enable it for your cluster.
-
You’ll get:
-
App ID (e.g.,
myapp-abcde
) -
Endpoint:
https://data.mongodb-api.com/app/<app-id>/endpoint/data/v1/action
-
API Key: Used for authentication.
-
4. Data API Endpoints
The Data API supports these actions:
Action | Endpoint | Description |
---|---|---|
Insert One | /insertOne |
Insert a single document |
Insert Many | /insertMany |
Insert multiple documents |
Find One | /findOne |
Find one document |
Find Many | /find |
Find multiple documents |
Update One | /updateOne |
Update a single document |
Update Many | /updateMany |
Update multiple documents |
Replace One | /replaceOne |
Replace an entire document |
Delete One | /deleteOne |
Delete a single document |
Delete Many | /deleteMany |
Delete multiple documents |
Aggregate | /aggregate |
Run aggregation pipelines |
5. Example: Insert a Document
Request:
curl --location \
'https://data.mongodb-api.com/app/<app-id>/endpoint/data/v1/action/insertOne' \
--header 'Content-Type: application/json' \
--header 'api-key: <YOUR_API_KEY>' \
--data '{
"dataSource": "Cluster0",
"database": "shopDB",
"collection": "products",
"document": {
"name": "iPhone 16",
"price": 1200,
"stock": 50
}
}'
Response:
{
"insertedId": "66b01e5b2fcf1c123abc456d"
}
6. Example: Find Documents
Request:
curl --location \
'https://data.mongodb-api.com/app/<app-id>/endpoint/data/v1/action/find' \
--header 'Content-Type: application/json' \
--header 'api-key: <YOUR_API_KEY>' \
--data '{
"dataSource": "Cluster0",
"database": "shopDB",
"collection": "products",
"filter": { "price": { "$gt": 500 } },
"limit": 5
}'
Response:
{
"documents": [
{ "name": "iPhone 16", "price": 1200, "stock": 50 },
{ "name": "MacBook Pro", "price": 2500, "stock": 10 }
]
}
7. Example: Update a Document
curl --location \
'https://data.mongodb-api.com/app/<app-id>/endpoint/data/v1/action/updateOne' \
--header 'Content-Type: application/json' \
--header 'api-key: <YOUR_API_KEY>' \
--data '{
"dataSource": "Cluster0",
"database": "shopDB",
"collection": "products",
"filter": { "name": "iPhone 16" },
"update": { "$set": { "stock": 40 } }
}'
8. Example: Aggregate Data
curl --location \
'https://data.mongodb-api.com/app/<app-id>/endpoint/data/v1/action/aggregate' \
--header 'Content-Type: application/json' \
--header 'api-key: <YOUR_API_KEY>' \
--data '{
"dataSource": "Cluster0",
"database": "shopDB",
"collection": "products",
"pipeline": [
{ "$match": { "price": { "$gt": 500 } } },
{ "$group": { "_id": null, "totalStock": { "$sum": "$stock" } } }
]
}'
Response:
{
"documents": [
{ "totalStock": 60 }
]
}
9. Using Data API in JavaScript
You can also use JavaScript fetch:
const url = "https://data.mongodb-api.com/app/<app-id>/endpoint/data/v1/action/find";
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"api-key": "<YOUR_API_KEY>"
},
body: JSON.stringify({
dataSource: "Cluster0",
database: "shopDB",
collection: "products",
filter: {}
})
};
const response = await fetch(url, options);
const data = await response.json();
console.log(data.documents);
10. Key Notes
-
Authentication → Always use the API Key or JWT.
-
Security → Restrict the API key in Atlas settings.
-
Rate Limits → Default is 500 requests/sec.
-
Best For → Serverless apps, mobile apps, Next.js, React, etc.