- Introduction to MongoDB >
- BSON Types >
- MongoDB Extended JSON (v1)
MongoDB Extended JSON (v1)¶
On this page
Important
Disambiguation
The following page discusses MongoDB Extended JSON v1 (Legacy extended JSON). For discussion on MongoDB Extended JSON v2, see MongoDB Extended JSON (v2).
For supported data types in mongo, see
mongosh Data Types.
For supported data types in the legacy mongo shell,
see Data Types in the Legacy mongo Shell.
JSON can only represent a subset of the types supported by BSON. To preserve type information, MongoDB adds the following extensions to the JSON format:
- Strict mode. Strict mode representations of BSON types conform to the JSON RFC. Any JSON parser can parse these strict mode representations as key/value pairs; however, only the MongoDB internal JSON parser recognizes the type information conveyed by the format.
mongoShell mode. The MongoDB internal JSON parser and themongoshell can parse this mode.
The representation used for the various data types depends on the context in which the JSON is parsed.
MongoDB Extended JSON v1 and MongoDB Drivers¶
The following drivers use the Extended JSON v1.0 (Legacy)
- C#
- Ruby
For the other drivers, refer to MongoDB Extended JSON (v2).
Parsers and Supported Format¶
Input in Strict Mode¶
The following can parse representations in strict mode with recognition of the type information.
mongoimportversion 4.0 and earlier--queryoption of various MongoDB tools- MongoDB Compass
Other JSON parsers, including mongo shell, can parse
strict mode representations as key/value pairs, but without
recognition of the type information.
Input in mongo Shell Mode¶
The following can parse representations in mongo shell mode with
recognition of the type information.
mongoimportversion 4.0 and earlier--queryoption of various MongoDB toolsmongoshell
Output in Strict mode¶
Before version 4.2, mongoexport outputs data in Strict mode
of MongoDB Extended JSON v1.
Output in mongo Shell Mode¶
Before version 4.2, bsondump outputs in mongo Shell
mode.
BSON Data Types and Associated Representations¶
The following presents the BSON data types and the associated
representations in Strict mode and mongo Shell mode.
Binary¶
-
data_binary¶ Strict Mode mongoShell Mode
Where the values are as follows:
<bindata>is the base64 representation of a binary string.<t>is a representation of a single byte indicating the data type. In Strict mode it is a hexadecimal string, and in Shell mode it is an integer. See the extended bson documentation. http://bsonspec.org/spec.html
Date¶
-
data_date¶ Strict Mode mongoShell ModeIn Strict mode,
<date>is an ISO-8601 date format with a mandatory time zone field following the templateYYYY-MM-DDTHH:mm:ss.mmm<+/-Offset>.In Shell mode,
<date>is the JSON representation of a 64-bit signed integer giving the number of milliseconds since epoch UTC.
Timestamp¶
-
data_timestamp¶ Strict Mode mongoShell Mode
Where the values are as follows:
<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 mongoShell Mode
Where the values are as follows:
<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 theJavaScriptandmongo Shellrepresentations support a limited range of options, any nonconforming options will be dropped when converting to this representation.
OID¶
-
data_oid¶ Strict Mode mongoShell Mode
Where the values are as follows:
<id>is a 24-character hexadecimal string.
DB Reference¶
-
data_ref¶ Strict Mode mongoShell Mode
Where the values are as follows:
<name>is a string of valid JSON characters.<id>is any valid extended JSON type.
Undefined Type¶
-
data_undefined¶ Strict Mode mongoShell ModeThe representation for the JavaScript/BSON undefined type.
You cannot use
undefinedin query documents. Consider the following document inserted into thepeoplecollection using the legacymongoshell:The following queries return an error:
However, you can query for undefined values using
$type, as in:This query returns all documents for which the
agefield has valueundefined.Important
The undefined BSON type is deprecated.
bin.mongoshstores a null value instead.For example, use the same code to insert a document in
bin.mongoshand in the legacymongoshell:The resulting documents are different:
MinKey¶
-
data_minkey¶ Strict Mode mongoShell ModeThe representation of the MinKey BSON data type that compares lower than all other types. See Comparison/Sort Order for more information on comparison order for BSON types.
MaxKey¶
-
data_maxkey¶ Strict Mode mongoShell ModeThe representation of the MaxKey BSON data type that compares higher than all other types. See Comparison/Sort Order for more information on comparison order for BSON types.
NumberLong¶
-
data_numberlong¶ Strict Mode mongoShell ModeNumberLongis a 64 bit signed integer. In the legacymongoshell, you must use quotation marks to insert aNumberLongor the operation will produce an error.For example, the following commands attempt to insert
9223372036854775807as aNumberLongwith and without quotation marks around the integer value:The highlighted line produces an error in the legacy
mongoshell. The insert succeeds inmongosh.
NumberDecimal¶
-
data_numberdecimal¶ Strict Mode mongoShell ModeNumberDecimalis a high-precision decimal. You must include quotation marks, or the input number will be treated as a double, resulting in data loss.For example, the following commands insert
123.40as aNumberDecimalwith and without quotation marks around the value:When you retrieve the documents, the value of
decimalUnQuotedhas changed, whiledecimalQuotedretains its specified precision:Important
This insert behavior is different in
bin.mongosh.The quoted string format,
NumberDecimal("123.40"), is deprecated. The insert succeeds, but also produces a warning.The unquoted string format,
NumberDecimal(123.40), stores the value as123.4. The trailing0is dropped.