OPTIONS

MongoDB Extended JSON

MongoDB import and export utilities (i.e. mongoimport and mongoexport) and MongoDB REST Interfaces render an approximation of MongoDB BSON documents in JSON format.

The REST interface supports three different modes for document output:

  • Strict mode that produces output that conforms to the JSON RFC specifications.
  • JavaScript mode that produces output that most JavaScript interpreters can process (via the --jsonp option)
  • mongo Shell mode produces output that the mongo shell can process. This is “extended” JavaScript format.

MongoDB can process these representations in REST input.

Special representations of BSON data in JSON format make it possible to render information that have no obvious corresponding JSON. In some cases MongoDB supports multiple equivalent representations of the same type information.

BSON Data Types and Associated Representations

The following presents the BSON data types and the associated representations in the three different modes.

Binary

data_binary
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$binary": "<bindata>",
  "$type": "<t>"
}
{
  "$binary": "<bindata>",
  "$type": "<t>"
}
BinData ( <t>, <bindata> )
  • <bindata> is the base64 representation of a binary string.
  • <t> is the hexadecimal representation of a single byte that indicates the data type.

Date

data_date
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$date": <date>
}
new Date( <date> )
new Date ( <date> )

<date> is the JSON representation of a 64-bit signed integer for milliseconds since epoch UTC (unsigned before version 1.9.1).

Timestamp

data_timestamp
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$timestamp": {
      "t": <t>,
      "i": <i>
  }
}
{
  "$timestamp": {
      "t": <t>,
      "i": <i>
  }
}
Timestamp( <t>, <i> )
  • <t> is the JSON representation of a 32-bit unsigned integer for seconds since epoch.
  • <i> is a 32-bit unsigned integer for the increment.

Regular Expression

data_regex
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$regex": "<sRegex>",
  "$options": "<sOptions>"
}
/<jRegex>/<jOptions>
/<jRegex>/<jOptions>
  • <sRegex> is a string of valid JSON characters.
  • <jRegex> is a string that may contain valid JSON characters and unescaped double quote (") characters, but may not contain unescaped forward slash (/) characters.
  • <sOptions> is a string containing the regex options represented by the letters of the alphabet.
  • <jOptions> is a string that may contain only the characters ‘g’, ‘i’, ‘m’ and ‘s’ (added in v1.9). Because the JavaScript and mongo Shell representations support a limited range of options, any nonconforming options will be dropped when converting to this representation.

OID

data_oid
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$oid": "<id>"
}
{
  "$oid": "<id>"
}
ObjectId( "<id>" )

<id> is a 24-character hexadecimal string.

DB Reference

data_ref
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$ref": "<name>",
  "$id": "<id>"
}
{
  "$ref" : "<name>",
  "$id" : "<id>"
}
DBRef("<name>", "<id>")
  • <name> is a string of valid JSON characters.
  • <id> is any valid extended JSON type.

Undefined Type

data_undefined
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$undefined": true
}
undefined
undefined

The representation for the JavaScript/BSON undefined type.

MinKey

data_minkey
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$minKey": 1
}
{
  "$minKey": 1
}
MinKey

The representation of the MinKey BSON data type that compares lower than all other types. See What is the compare order for BSON types? for more information on comparison order for BSON types.

MaxKey

data_maxkey
Strict Mode JavaScript Mode (via JSONP) mongo Shell Mode
{
  "$maxKey": 1
}
{
  "$maxKey": 1
}
MaxKey

The representation of the MaxKey BSON data type that compares higher than all other types. See What is the compare order for BSON types? for more information on comparison order for BSON types.