On this page本页内容
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 mongo
shell wrapper methods for types, see Data Types in the 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:
mongo
Shell mode. The MongoDB internal JSON parser and the mongo
shell can parse this mode.The representation used for the various data types depends on the context in which the JSON is parsed.
The following drivers use the Extended JSON v1.0 (Legacy)
For the other drivers, refer to MongoDB Extended JSON (v2).
The following can parse representations in strict mode with recognition of the type information.
mongoimport
version 4.0 and earlier--query
option of various MongoDB toolsOther JSON parsers, including mongo
shell, can parse strict mode representations as key/value pairs, but without
recognition of the type information.
mongo
Shell Mode¶The following can parse representations in mongo
shell mode with
recognition of the type information.
mongoimport
version 4.0 and earlier--query
option of various MongoDB toolsmongo
shellBefore version 4.2, mongoexport
outputs data in Strict mode
of MongoDB Extended JSON v1.
The following presents the BSON data types and the associated representations in Strict mode and mongo
Shell mode.
<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.htmldata_date
¶Strict Mode | mongo Shell Mode | |
---|---|---|
In Strict mode, <date>
is an ISO-8601 date format with a mandatory time zone field following the template YYYY-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.
<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.<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.<name>
is a string of valid JSON characters.<id>
is any valid extended JSON type.data_undefined
¶Strict Mode | mongo Shell Mode | |
---|---|---|
The representation for the JavaScript/BSON undefined type.
You cannot use undefined
in query documents. Consider the following document inserted into the people
collection:
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 age
field has value undefined
.
data_minkey
¶Strict Mode | mongo Shell Mode | |
---|---|---|
The 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.
data_maxkey
¶Strict Mode | mongo Shell Mode | |
---|---|---|
The 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.
data_numberlong
¶Strict Mode | mongo Shell Mode | |
---|---|---|
NumberLong
is a 64 bit signed integer. You must include quotation marks or it will be interpreted as a floating point number, resulting in a loss of accuracy.
For example, the following commands insert 9223372036854775807
as a NumberLong
with and without quotation marks around the integer value:
When you retrieve the documents, the value of longUnQuoted
has changed, while longQuoted
retains its accuracy:
New in version 3.4.版本3.4中的新功能。
data_numberdecimal
¶Strict Mode | mongo Shell Mode | |
---|---|---|
NumberDecimal
is 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.40
as a NumberDecimal
with and without quotation marks around the value:
When you retrieve the documents, the value of decimalUnQuoted
has changed, while decimalQuoted
retains its specified precision: