Data API Examples
The following examples demonstrate how to send requests to the Atlas Data API.
Specify BSON Types in Requests
Data API requests can specify BSON types that don't exist in JSON by instead using the EJSON data format in the request body. You can use EJSON to match BSON types in query filters or write BSON types in insert and update operations.
To specify that a request body uses EJSON, set the Content-Type
header:
Content-Type: application/ejson
Binary
To specify a binary value, use $binary
with the value encoded in
Base64 and a BSON subtype encoded as
a two-character hexadecimal string:
curl --request POST \ 'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/insertOne' \ --header 'Content-Type: application/ejson' \ --header 'api-key: <Data API Key>' \ --data-raw '{ "dataSource": "<cluster name>", "database": "<database name>", "collection": "<collection name>", "document": { "data": { "$binary": { "base64": "46d989eaf0bde5258029534bc2dc2089", "subType": "05" } } } }'
Date
To specify a date, use $date
with the UNIX timestamp in
milliseconds as a 64-bit integer:
curl --request POST \ 'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/insertOne' \ --header 'Content-Type: application/ejson' \ --header 'api-key: <Data API Key>' \ --data-raw '{ "dataSource": "<cluster name>", "database": "<database name>", "collection": "<collection name>", "document": { "createdAt": { "$date": { "$numberLong": "1638551310749" } } } }'
Decimal128
To specify a 128-bit decimal, use $numberDecimal
with the decimal
value as a string:
curl --request POST \ 'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/insertOne' \ --header 'Content-Type: application/ejson' \ --header 'api-key: <Data API Key>' \ --data-raw '{ "dataSource": "<cluster name>", "database": "<database name>", "collection": "<collection name>", "document": { "accountBalance": { "$numberDecimal": "128452.420523" } } }'
Double
To specify a 64-bit signed floating point value (commonly referred to as
a "double"), use $numberDouble
with the integer value as a string:
curl --request POST \ 'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/insertOne' \ --header 'Content-Type: application/ejson' \ --header 'api-key: <Data API Key>' \ --data-raw '{ "dataSource": "<cluster name>", "database": "<database name>", "collection": "<collection name>", "document": { "temperatureCelsius": { "$numberDouble": "23.847" } } }'
Int32
To specify a 32-bit signed integer value, use $numberInt
with the
integer value as a string:
curl --request POST \ 'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/insertOne' \ --header 'Content-Type: application/ejson' \ --header 'api-key: <Data API Key>' \ --data-raw '{ "dataSource": "<cluster name>", "database": "<database name>", "collection": "<collection name>", "document": { "coins": { "$numberInt": "2147483647" } } }'
Int64
To specify a 64-bit signed integer value, use $numberLong
with the
integer value as a string:
curl --request POST \ 'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/insertOne' \ --header 'Content-Type: application/ejson' \ --header 'api-key: <Data API Key>' \ --data-raw '{ "dataSource": "<cluster name>", "database": "<database name>", "collection": "<collection name>", "document": { "population": { "$numberLong": "8047923148" } } }'
ObjectId
To specify an ObjectId value, use $oid
with the ID as a byte string:
curl --request POST \ 'https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/insertOne' \ --header 'Content-Type: application/ejson' \ --header 'api-key: <Data API Key>' \ --data-raw '{ "dataSource": "<cluster name>", "database": "<database name>", "collection": "<collection name>", "document": { "_id": { "$oid": "61f02ea3af3561e283d06b91" } } }'