🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to MongoDB Notes
Topic #24

MongoDB Data API


MongoDB Data API

The MongoDB Data API can be used to query and update data in a MongoDB database without the need for language specific drivers.

Note: Language drivers should be used when possible, but the MongoDB Data API comes in handy when drivers are not available or drivers are overkill for the application.


Read & Write with the MongoDB Data API

The MongoDB Data API is a pre-configured set of HTTPS endpoints that can be used to read and write data to a MongoDB Atlas database.

With the MongoDB Data API, you can create, read, update, delete, or aggregate documents in a MongoDB Atlas database.


Cluster Configuration

In order to use the Data API, you must first enable the functionality from the Atlas UI.

From the MongoDB Atlas dashboard, navigate to Data API in the left menu.

Select the data source(s) you would like to enable the API on and click Enable the Data API.


Access Level

By default, no access is granted. Select the access level you'd like to grant the Data API. The choices are: No Access, Read Only, Read and Write, or Custom Access.


Data API Key

In order to authenticate with the Data API, you must first create a Data API key.

Click Create API Key, enter a name for the key, then click Generate API Key.

Be sure to copy the API key and save it somewhere safe. You will not get another chance to see this key again.


Sending a Data API Request

We can now use the Data API to send a request to the database.

In the next example, we'll use curl to find the first document in the movies collection of our sample_mflix database. We loaded this sample data in the Intro to Aggregations section.

To run this example, you'll need your App Id, API Key, and Cluster name.

You can find your App Id in the URL Endpoint field of the Data API page in the MongoDB Atlas UI.

Example

curl --location --request POST 'https://data.mongodb-api.com/app/<DATA API APP ID>/endpoint/data/v1/action/findOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <DATA API KEY>' \
--data-raw '{
    "dataSource":"<CLUSTER NAME>",
    "database":"sample_mflix",
    "collection":"movies",
    "projection": {"title": 1}
}'

Data API Endpoints

In the previous example, we used the findOne endpoint in our URL.

There are several endpoints available for use with the Data API.

All endpoints start with the Base URL: https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/


Find a Single Document

Endpoint

The findOne endpoint is used to find a single document in a collection.

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "filter": <query filter>,
  "projection": <projection>
}

Find Multiple Documents

Endpoint

The find endpoint is used to find multiple documents in a collection.

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "filter": <query filter>,
  "projection": <projection>,
  "sort": <sort expression>,
  "limit": <number>,
  "skip": <number>
}

Insert a Single Document

Endpoint

The insertOne endpoint is used to insert a single document into a collection.

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "document": <document>
}

Insert Multiple Documents

Endpoint

The insertMany endpoint is used to insert multiple documents into a collection.

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "documents": [<document>, <document>, ...]
}

Update a Single Document

Endpoint

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "filter": <query filter>,
  "update": <update expression>,
  "upsert": true|false
}

Update Multiple Documents

Endpoint

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "filter": <query filter>,
  "update": <update expression>,
  "upsert": true|false
}

Delete a Single Document

Endpoint

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "filter": <query filter>
}

Delete Multiple Documents

Endpoint

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "filter": <query filter>
}

Aggregate Documents

Endpoint

Request Body

Example

{
  "dataSource": "<data source name>",
  "database": "<database name>",
  "collection": "<collection name>",
  "pipeline": [<pipeline expression>, ...]
}

Want to go beyond the notes?

Join CodingNow 2.0's MongoDB course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

MongoDB Data API – FAQs

Quick answers about learning MongoDB Data API in MongoDB.

This free note from CodingNow 2.0 explains MongoDB Data API in MongoDB — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every MongoDB topic on CodingNow 2.0, including MongoDB Data API, is 100% free with no signup required.
With focused practice, most students grasp MongoDB Data API in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now