Data API Data Formats
On this page
MongoDB stores data in a format called BSON, which is similar to a JSON object in structure but supports additional data types and uses a binary encoding. BSON is efficient for computers but is not human readable, so you can't work with it directly.
Instead, the Data API uses two formats to represent data in requests and responses:
The JSON format uses standard types that any tool can parse and understand. However, JSON cannot represent all BSON types, so JSON responses may lose type information for some fields. For example, BSON has distinct types for 32-bit integers and 64-bit floats, but a JSON response represents both as a
number
.The EJSON format, short for MongoDB Extended JSON, is a superset of standard JSON that uses structured fields to represent BSON data that don't have corresponding JSON types. This fully represents your data but requires your client to understand how to work with EJSON.
Example
This document can be represented in either JSON or EJSON shows BSON types represented in JSON and EJSON:
You define a single default return type for all generated Data API
endpoints and individually for each custom endpoint. Incoming requests
can also specify a preferred data format that overrides the default
using an Accept
header. To learn more, see Choose a Response
Data Format.
BSON Types
This sections lists the BSON types that the Data API supports and shows how each type is represented in JSON and EJSON format.
Array
EJSON | JSON | ||
---|---|---|---|
|
|
Binary
EJSON | JSON | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Date
EJSON | JSON | ||||||
---|---|---|---|---|---|---|---|
|
|
Decimal128
EJSON | JSON | ||
---|---|---|---|
|
|
Document
EJSON | JSON | ||
---|---|---|---|
|
|
Double
EJSON | JSON | ||||
---|---|---|---|---|---|
|
|
Int32
EJSON | JSON | ||
---|---|---|---|
|
|
Int64
EJSON | JSON | ||
---|---|---|---|
|
|
MaxKey
EJSON | JSON | ||
---|---|---|---|
|
No JSON equivalent |
MinKey
EJSON | JSON | ||
---|---|---|---|
|
No JSON equivalent |
ObjectId
EJSON | JSON | ||||
---|---|---|---|---|---|
|
|
Regular Expression
EJSON | JSON | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Timestamp
EJSON | JSON | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|