Docs Menu

Data API Resources

On this page

Important

Each Atlas API has its own resources and requires initial setup. The Data API also uses different access keys from the Atlas Administration API and the Realm Admin API.

To learn more, see APIs.

All Data API endpoints have the following base URL, where <Data API App ID> is a unique ID specific to each cluster:

https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta
POST /action/findOne
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/findOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"filter": {
"text": "Do the dishes"
}
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"filter": <query filter>,
"projection": <projection>
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
filter
object
Optional

A MongoDB Query Filter. The findOne action returns the first document in the collection that matches this filter.

If you do not specify a filter, the action matches all document in the collection.

{}
projection
object
Optional
A MongoDB Query Projection. Depending on the projection, the returned document will either omit specific fields or include only specified fields or values
{}

The findOne action returns the matched document in the document field:

{ "document": { "_id": "6193504e1be4ab27791c8133", "test": "foo" } }

If the action does not match any documents, the document field is null:

{ "document": null }
POST /action/find
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/find' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"filter": {
"status": "complete"
},
"sort": { "completedAt": 1 },
"limit": 10
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"filter": <query filter>,
"projection": <projection>,
"sort": <sort expression>,
"limit": <number>,
"skip": <number>
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
filter
object
Optional

A MongoDB Query Filter. The find action returns documents in the collection that match this filter.

If you do not specify a filter, the action matches all document in the collection.

If the filter matches more documents than the specified limit, the action only returns a subset of them. You can use skip in subsequent queries to return later documents in the result set.

{}
projection
object
Optional
A MongoDB Query Projection. Depending on the projection, the returned documents either omit specific fields or include only specified fields and values.
{}
sort
object
Optional
A MongoDB Sort Expression. Matched documents are returned in ascending or descending order of the fields specified in the expression.
{}
limit
number
Optional
The maximum number of matched documents to include in the returned result set. Each request may return up to 50,000 documents.
1000
skip
number
Optional
The number of matched documents to skip before adding matched documents to the result set.
0

The find action returns an array of matched document in the documents field:

{
"documents": [
{ "_id": "6193504e1be4ab27791c8133", ... },
{ "_id": "6194604e1d38dc33792d8257", ... }
]
}

If the action does not match any documents, the documents field is an empty array:

{ "documents": [] }
POST /action/insertOne
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/insertOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"document": {
"status": "open",
"text": "Do the dishes"
}
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"document": { ... },
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
document
object
Required
An EJSON document to insert into the collection.

The insertOne action returns the _id value of the inserted document as a string in the insertedId field:

{ "insertedId": "6193504e1be4ab27791c8133" }
POST /action/insertMany
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/insertMany' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"documents": [
{ "status": "open", "text": "Mop the floor" },
{ "status": "open", "text": "Clean the windows" }
]
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"documents": [{ ... }, ...]
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
documents
array of objects
Required
An array of one or more EJSON documents to insert into the collection.

The insertMany action returns the _id values of all inserted documents as an array of strings in the insertedIds field:

{ "insertedIds": ["61935189ec53247016a623c9", "61935189ec53247016a623ca"] }
POST /action/updateOne
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/updateOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"filter": { "_id": { "$oid": "6193ebd53821e5ec5b4f6c3b" } },
"update": {
"$set": {
"status": "complete",
"completedAt": { "$date": { "$numberLong": 1637083942954 } }
}
}
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"filter": { ... },
"update": { ... },
"upsert": true|false
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
filter
object
Required
A MongoDB Query Filter. The updateOne action modifies the first document in the collection that matches this filter.
update
object
Required
A MongoDB Update Expression that specifies how to modify the matched document.
upsert
boolean
Optional
The upsert flag only applies if no documents match the specified filter. If true, the updateOne action inserts a new document that matches the filter with the specified update applied to it.
false

The updateOne action returns:

  • the number of documents that the filter matched in the matchedCount field
  • the number of matching documents that were updated in the modifiedCount field
{ "matchedCount": 1, "modifiedCount": 1 }

If upsert is set to true and no documents match the filter, the action returns the _id value of the inserted document as a string in the upsertedId field:

{ "matchedCount": 0, "modifiedCount": 0, "upsertedId": "619353593821e5ec5b0f8944" }
POST /action/updateMany
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/updateMany' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"filter": { "status": "open" },
"update": {
"$set": {
"status": "complete",
"completedAt": { "$date": { "$numberLong": 1637083942954 } }
}
}
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"filter": { ... },
"update": { ... },
"upsert": true|false
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
filter
object
Required
A MongoDB Query Filter. The updateMany action modifies all documents in the collection that match this filter.
update
object
Required
A MongoDB Update Expression that specifies how to modify matched documents.
upsert
boolean
Optional
The upsert flag only applies if no documents match the specified filter. If true, the updateMany action inserts a new document that matches the filter with the specified update applied to it.
false

The updateMany action returns:

  • the number of documents that the filter matched in the matchedCount field
  • the number of matching documents that were updated in the modifiedCount field
{ "matchedCount": 12, "modifiedCount": 12 }

If upsert is set to true and no documents match the filter, the action returns the _id value of the inserted document as a string in the upsertedId field:

{ "matchedCount": 0, "modifiedCount": 0, "upsertedId": "619353593821e5ec5b0f8944" }
POST /action/replaceOne
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/replaceOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"filter": { "text": "Call Jessica" },
"replacement": {
"status": "open",
"text": "Call Amy"
}
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"filter": { ... },
"replacement": { ... },
"upsert": true|false
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
filter
object
Required
A MongoDB Query Filter. The replaceOne action overwrites the first document in the collection that matches this filter.
replacement
object
Required
An EJSON document that overwrites the matched document.
upsert
boolean
Optional
The upsert flag only applies if no documents match the specified filter. If true, the replaceOne action inserts the replacement document.
false

The replaceOne action returns:

  • the number of documents that the filter matched in the matchedCount field
  • the number of matching documents that were replaced in the modifiedCount field
{ "matchedCount": 1, "modifiedCount": 1 }

If upsert is set to true and no documents match the filter, the action returns the _id value of the inserted document as a string in the upsertedId field:

{ "matchedCount": 0, "modifiedCount": 0, "upsertedId": "619353593821e5ec5b0f8944" }
POST /action/deleteOne
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/deleteOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"filter": { "_id": { "$oid": "6193ebd53821e5ec5b4f6c3b" } }
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"filter": { ... }
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
filter
object
Required
A MongoDB Query Filter. The deleteOne action deletes the first document in the collection that matches this filter.

The deleteOne action returns the number of deleted documents in the deletedCount field:

{ "deletedCount": 1 }
POST /action/deleteMany
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/deleteMany' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"filter": { "status": "complete" }
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"filter": { ... }
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
filter
object
Required
A MongoDB Query Filter. The deleteMany action deletes all documents in the collection that match this filter.

The deleteMany action returns the number of deleted documents in the deletedCount field:

{ "deletedCount": 42 }
POST /action/aggregate
curl --request POST \
'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/beta/action/aggregate' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <Data API Key>' \
--data-raw '{
"dataSource": "Cluster0",
"database": "todo",
"collection": "tasks",
"pipeline": [
{
"$group": {
"_id": "$status",
"count": { "$sum": 1 },
"text": { "$push": "$text" }
}
},
{ "$sort": "$count" }
]
}'
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"pipeline": [<aggregation pipeline>]
}
Name
Type
Necessity
Description
Default
dataSource
string
Required
The name of the cluster.
database
string
Required
The name of the database.
collection
string
Required
The name of the collection.
pipeline
array of objects
Required

The aggregate action returns the result set of the final stage of the pipeline as an array of documents in the documents field:

{ "documents": [{ ... }, ...] }
Code
Description
400

Bad request

The request was invalid. This might mean:

  • A request header is missing.
  • The request body is malformed or improperly encoded.
  • A field has a value with an invalid type.
  • The specified data source is disabled or does not exist.
  • The specified database or collection does not exist.
401

Unauthorized

The request did not include an authorized and enabled Data API Key. Ensure that your Data API Key is enabled for the cluster.

404

Not found

The request was sent to an endpoint that does not exist.

500s

Server errors

The Data API encountered an internal error and could not complete the request.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.