MongoDB Extended JSON (v2)

Disambiguation

The following page discusses MongoDB Extended JSON v2. Fordiscussion on the Legacy MongoDB Extended JSON v1, seeMongoDB Extended JSON (v1).

For mongo shell wrapper methods for types, seeData Types in the mongo Shell.

JSON can only directly represent a subset of the typessupported by BSON. To preserve type information, MongoDB addsthe following extensions to the JSON format.

    • Canonical Mode
    • A string format that emphasizes type preservation at the expenseof readability and interoperability. That is, conversion fromcanonical to BSON will generally preserve type information exceptin certain specific cases.
    • Relaxed Mode
    • A string format that emphasizes readability and interoperabilityat the expense of type preservation. That is, conversion fromrelaxed format to BSON can lose type information.

Both formats conform to the JSON RFC and can beparsed by the various MongoDB drivers and tools.

MongoDB Extended JSON v2 Usage

Drivers

The following drivers use the Extended JSON v2.0

- C- C++- Go- Java- Node- Perl- PHPC- Python- Scala

For C# and Ruby that use Legacy MongoDB Extended JSON v1, refer toMongoDB Extended JSON (v1).

MongoDB Command-Line Tools

Starting in version 4.2:

bsondumpUses Extended JSON v2.0 (Canonicalmode) format.
mongodumpUse Extended JSON v2.0 (Canonical mode) format for themetadata. Requires mongorestore version 4.2+that supports Extended JSON v2.0 (Canonical mode orRelaxed) format.TipIn general, use corresponding versions ofmongodump and mongorestore.That is, to restore data files created with a specificversion of mongodump, use the correspondingversion of mongorestore.
mongoexportCreates output data in Extended JSON v2.0 (Relaxed mode) bydefault.Creates output data in Extended JSON v2.0 (Canonical mode) ifused with —jsonFormat canonical.
mongoimportExpects import data to be in Extended JSON v2.0 (eitherRelaxed or Canonical mode) by default.Can recognize data that is in Extended JSON v1.0 format if the option—legacy is specified.TipIn general, the versions of mongoexport andmongoimport should match. That is, to importdata created from mongoexport, you should usethe corresponding version of mongoimport.

BSON Data Types and Associated Representations

The following presents some common BSON data types and the associatedrepresentations in Canonical and Relaxed.

- Array- Binary- Date- Decimal128- Document- Double- Int32- Int64- MaxKey- MinKey- ObjectId- Regular Expression- Timestamp

For a complete list, seehttps://github.com/mongodb/specifications/blob/master/source/extended-json.rst#conversion-table.

  • Array

CanonicalRelaxed
  1. [ <elements> ]
  1. <Same as Canonical>
Where the array elements are as follows:
- - <elements> - Array elements use Extended JSON.To specify an empty array, omit the content [ ].
  • Binary

CanonicalRelaxed
  1. { "$binary": { "base64": "<payload>", "subType": "<t>" }}
  1. <Same as Canonical>
Where the values are as follows:
- - "<payload>" - Base64 encoded (with padding as “=”) payload string.- - "<t>" - A one- or two-character hex string that corresponds to aBSON binary subtype. See the extended bson documentationhttp://bsonspec.org/spec.html for subtypes available.
  • Date

For dates between years 1970 and 9999, inclusive:

CanonicalRelaxed
  1. {"$date": {"$numberLong": "<millis>"}}
  1. {"$date": "<ISO-8601 Date/Time Format>"}

For dates before year 1970 or after year 9999:

CanonicalRelaxed
  1. {"$date": {"$numberLong": "<millis>"}}
  1. <Same as Canonical>
Where the values are as follows:
- - "<millis>" - A 64-bit signed integer as string. The valuerepresents milliseconds relative to the epoch.- - "<ISO-8601 Date/Time Format>" - A date in ISO-8601 InternetDate/Time Format as string.The date/time has a maximum time precision of milliseconds: - Fractional seconds have exactly 3 decimalplaces if the fractional part is non-zero. - Otherwise, fractional seconds SHOULD be omitted if zero.
  • Decimal128

New in version 3.4.

CanonicalRelaxed
  1. { "$numberDecimal": "<number>" }
  1. <Same as Canonical>
Where the values are as follows:
- - "<number>" - A high-precision decimalas string.
  • Document

CanonicalRelaxed
  1. { <content> }
  1. <Same as Canonical>
Where the document contents are as follows:
- - <content> - Name:value pairs that use Extended JSON.To specify an empty document, omit the content { }.
  • Double

For finite numbers:

CanonicalRelaxed
  1. {"$numberDouble": "<decimal string>" }
  1. <non-integer number>

For infinite numbers or NAN:

CanonicalRelaxed
  1. {"$numberDouble": <"Infinity"|"-Infinity"|"NaN"> }
  1. <Same as Canonical>
Where the values are as follows:
- - "<decimal string>" - A 64-bit signed floating point as a string.- - <non-integer number> - A non-integer number. Integer numbers are parsed asinteger instead of double.
  • Int64

CanonicalRelaxed
  1. { "$numberLong": "<number>" }
  1. <integer>
Where the values are as follows:
- - "<number>" - A 64-bit signed integer as string.- - <integer> - A 64-bit signed integer.
  • Int32

CanonicalRelaxed
  1. { "$numberInt": "<number>" }
  1. <integer>
Where the values are as follows:
- - "<number>" - A 32-bit signed integer as string.- - <integer> - A 32-bit signed integer.
  • MaxKey

CanonicalRelaxed
  1. { "$maxKey": 1 }
  1. <Same as Canonical>

The MaxKey BSON data type compares higher than all other types. SeeComparison/Sort Order for more information oncomparison order for BSON types.

  • MinKey

CanonicalRelaxed
  1. { "$minKey": 1 }
  1. <Same as Canonical>

The MinKey BSON data type compares lower than all other types. SeeComparison/Sort Order for more information oncomparison order for BSON types.

  • ObjectId

CanonicalRelaxed
  1. { "$oid": "<ObjectId bytes>" }
  1. <Same as Canonical>
Where the values are as follows:
- - "<ObjectId bytes>" - A 24-character, big-endian hexadecimal string thatrepresents the ObjectId bytes.
  • Regular Expression

CanonicalRelaxed
  1. { "$regularExpression": { "pattern": "<regexPattern>", "options": "<options>" }}
  1. <Same as Canonical>
Where the values are as follows:
- - "<regexPattern>" -A string that corresponds to the regular expressionpattern. The string can contain valid JSON characters andunescaped double quote (") characters, but may notcontain unescaped forward slash (/) characters.- - "<options>" -A string that specifies BSON regular expression options(‘g’, ‘i’, ‘m’ and ‘s’) or an empty string "".Options other than (‘g’, ‘i’, ‘m’ and ‘s’) will be droppedwhen converting to this representation.ImportantThe options MUST be in alphabetical order.
  • Timestamp

CanonicalRelaxed
  1. {"$timestamp": {"t": <t>, "i": <i>}}
  1. <Same as Canonical>
Where the values are as follows:
- - <t> - A positive integer for the seconds since epoch.- - <i> - A positive integer for the increment.

Example

Example Field NameCanonical FormatRelaxed Format
“_id:”{“$oid”:”5d505646cf6d4fe581014ab2”}{“$oid”:”5d505646cf6d4fe581014ab2”}
“arrayField”:[“hello”,{“$numberInt”:”10”}][“hello”,10]
“dateField”:{“$date”:{“$numberLong”:”1565546054692”}}{“$date”:”2019-08-11T17:54:14.692Z”}
“dateBefore1970”:{“$date”:{“$numberLong”:”-1577923200000”}}{“$date”:{“$numberLong”:”-1577923200000”}}
“decimal128Field”:{“$numberDecimal”:”10.99”}{“$numberDecimal”:”10.99”}
“documentField”:{“a”:”hello”}{“a”:”hello”}
“doubleField”:{“$numberDouble”:”10.5”}10.5
“infiniteNumber”{“$numberDouble”:”Infinity”}{“$numberDouble”:”Infinity”}
“int32field”:{“$numberInt”:”10”}10
“int64Field”:{“$numberLong”:”50”}50
“minKeyField”:{“$minKey”:1}{“$minKey”:1}
“maxKeyField”:{“$maxKey”:1}{“$maxKey”:1}
“regexField”:{“$regularExpression”:{“pattern”:”^H”,”options”:”i”}}{“$regularExpression”:{“pattern”:”^H”,”options”:”i”}}
“timestampField”:{“$timestamp”:{“t”:1565545664,”i”:1}}{“$timestamp”:{“t”:1565545664,”i”:1}}