MongoDB Extended JSON (v2)
最后更新于
最后更新于
On this page
can only represent a subset of the types supported by. To preserve type information, MongoDB adds the following extensions to the JSON format:
Strict mode
. Strict mode representations of BSON types conform to the
. 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.
mongo
Shell mode
. The MongoDB internal JSON parser and the
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 can parse representations in strict mode_with_recognition of the type information.
--query
option of various MongoDB tools
mongo
Shell ModeThe following can parse representations inmongo
shell mode_with_recognition of the type information.
--query
option of various MongoDB tools
shell
mongo
Shell ModeThe following presents the BSON data types and the associated representations in_Strict mode_andmongo
_Shell mode_.
data_binary
Strict Mode
Shell Mode
{ "$binary": "<bindata>", "$type": "<t>" }
BinData ( <t>, <bindata> )
<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.
data_date
Strict Mode
Shell Mode
{ "$date": "<date>" }
new Date ( <date> )
In_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.
data_timestamp
Strict Mode
Shell Mode
{ "$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.
data_regex
Strict Mode
Shell Mode
{ "$regex": "<sRegex>", "$options": "<sOptions>" }
/<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.
data_oid
Strict Mode
Shell Mode
{ "$oid": "<id>" }
ObjectId( "<id>" )
<id>
is a 24-character hexadecimal string.
data_ref
Strict Mode
Shell Mode
{ "$ref": "<name>", "$id": "<id>" }
DBRef("<name>", "<id>")
<
name
>
is a string of valid JSON characters.
<
id
>
is any valid extended JSON type.
data_undefined
Strict Mode
Shell Mode
{ "$undefined": true }
undefined
The representation for the JavaScript/BSON undefined type.
You_cannot_useundefined
in query documents. Consider the following document inserted into thepeople
collection:
The following queries return an error:
This query returns all documents for which theage
field has valueundefined
.
data_minkey
Strict Mode
Shell Mode
{ "$minKey": 1 }
MinKey
data_maxkey
Strict Mode
Shell Mode
{ "$maxKey": 1 }
MaxKey
New in version 2.6.
data_numberlong
Strict Mode
Shell Mode
{ "$numberLong": "<number>" }
NumberLong( "<number>" )
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 insert9223372036854775807
as aNumberLong
with and without quotation marks around the integer value:
When you retrieve the documents, the value oflongUnQuoted
has changed, whilelongQuoted
retains its accuracy:
New in version 3.4.
data_numberdecimal
Strict Mode
Shell Mode
{ "$numberDecimal": "<number>" }
NumberDecimal( "<number>" )
For example, the following commands insert123.40
as aNumberDecimal
with and without quotation marks around the value:
When you retrieve the documents, the value ofdecimalUnQuoted
has changed, whiledecimalQuoted
retains its specified precision:
Other JSON parsers, includingshell and, can parse strict mode representations as key/value pairs, but_without_recognition of the type information.
andoutput data in_Strict mode_.
outputs inmongo
Shell mode.
The MongoDB JSON parser currently does not support loading ISO-8601 strings representing dates prior to the. When formatting pre-epoch dates and dates past what your system’stime_t
type can hold, the following format is used:
However, you can query for undefined values using, as in:
The representation of the MinKey BSON data type that compares lower than all other types. Seefor more information on comparison order for BSON types.
The representation of the MaxKey BSON data type that compares higher than all other types. Seefor more information on comparison order for BSON types.
NumberDecimal
is a. You must include quotation marks, or the input number will be treated as a double, resulting in data loss.