Docs HomeMongoDB Manual

MongoDB Extended JSON (v1)

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).下一页讨论MongoDB Extended JSON v1(Legacy Extended JSON)。有关MongoDB扩展JSON v2的讨论,请参阅MongoDB扩展的JSON(v2)

For supported data types in mongo, see mongosh Data Types.有关mongo中支持的数据类型,请参阅mongosh数据类型

For supported data types in the legacy mongo shell, see /core/shell-types.有关旧版mongo shell中支持的数据类型,请参阅/core/shell-types

JSON can only represent a subset of the types supported by BSON. 只能表示BSON支持的类型的子集。To preserve type information, MongoDB adds the following extensions to the JSON format:为了保留类型信息,MongoDB在JSON格式中添加了以下扩展:

  • Strict mode严格模式. Strict mode representations of BSON types conform to the JSON RFC. 。BSON类型的严格模式表示符合JSON RFCAny 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.任何JSON解析器都可以将这些严格的模式表示解析为键/值对;然而,只有MongoDB内部JSON解析器能够识别该格式所传递的类型信息。
  • mongo Shell mode. The MongoDB internal JSON parser and the mongo shell can parse this mode.MongoDB内部的JSON解析器和mongoshell可以解析这种模式。

The representation used for the various data types depends on the context in which the JSON is parsed.用于各种数据类型的表示取决于解析JSON的上下文。

MongoDB Extended JSON v1 and MongoDB DriversMongoDB扩展JSON v1和MongoDB驱动程序

The following drivers use the Extended JSON v1.0 (Legacy)以下驱动程序使用Extended JSON v1.0(Legacy)

  • C#
  • Ruby

For the other drivers, refer to MongoDB Extended JSON (v2).关于其他驱动程序,请参阅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.以下可以通过识别类型信息以严格模式解析表示。

  • mongoimport version 4.0 and earlier4.0及更早版本
  • --query option of various MongoDB tools各种MongoDB工具的--query选项
  • MongoDB Compass

Other JSON parsers, including mongo shell, can parse strict mode representations as key/value pairs, but without recognition of the type information.其他JSON解析器,包括mongoshell,可以将严格的模式表示解析为键/值对,但不需要识别类型信息。

Input in mongo Shell Modemongo Shell模式下的输入

The following can parse representations in mongo shell mode with recognition of the type information.以下内容可以在mongoshell模式下解析表示,并识别类型信息。

  • mongoimport version 4.0 and earlier4.0及更早版本
  • --query option of various MongoDB tools各种MongoDB工具的--query选项
  • mongo shell

Output in Strict mode以严格模式输出

Before version 4.2, mongoexport outputs data in Strict mode of MongoDB Extended JSON v1.在4.2版本之前,mongoexport以MongoDB Extended JSON v1的Strict模式输出数据。

Output in mongo Shell Modemongo Shell模式下的输出

Before version 4.2, bsondump outputs in mongo Shell mode.在4.2版本之前,bsondumpmongoshell模式下输出。

BSON Data Types and Associated RepresentationsBSON数据类型和关联表示

The following presents the BSON data types and the associated representations in Strict mode and mongo Shell mode.下面介绍了在Strict模式和mongo-Shell模式下的BSON数据类型和相关表示。

Binary二进制的

data_binary
Strict Mode严格模式mongo Shell Mode
{ "$binary": "<bindata>", "$type": "<t>" }
BinData ( <t>, <bindata> )

Where the values are as follows:其中值如下:

  • <bindata> is the base64 representation of a binary string.是二进制字符串的base64表示。
  • <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. 严格模式下,它是一个十六进制字符串,在Shell模式下,是一个整数。请参阅扩展的bson文档。http://bsonspec.org/spec.html

Date

data_date
Strict Mode严格模式mongo 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 template YYYY-MM-DDTHH:mm:ss.mmm<+/-Offset>.严格模式下,<date>是ISO-8601日期格式,模板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.在Shell模式中,<date>是一个64位带符号整数的JSON表示,给出自epoch UTC以来的毫秒数。

Timestamp

data_timestamp
Strict Mode严格模式mongo Shell Mode
{ "$timestamp": { "t": <t>, "i": <i> } }
Timestamp( <t>, <i> )

Where the values are as follows:其中值如下:

  • <t> is the JSON representation of a 32-bit unsigned integer for seconds since epoch.是自epoch以来秒的32位无符号整数的JSON表示形式。
  • <i> is a 32-bit unsigned integer for the increment.是增量的32位无符号整数。

Regular Expression正则表达式

data_regex
Strict Mode严格模式mongo Shell Mode
{ "$regex": "<sRegex>", "$options": "<sOptions>" }
/<jRegex>/<jOptions>

Where the values are as follows:其中值如下:

  • <sRegex> is a string of valid JSON characters.是一个有效的JSON字符字符串。
  • <jRegex> is a string that may contain valid JSON characters and unescaped double quote (") characters, but may not contain unescaped forward slash (/) characters.是一个字符串,该字符串可能包含有效的JSON字符和未转义的双引号(")字符,但可能不包含未转义的正斜杠(/)字符。
  • <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). 是一个字符串,可能只包含字符'g'、'i'、'm'和's'(在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.由于JavaScriptmongoShell表示支持的选项范围有限,因此在转换为此表示时,任何不一致的选项都将被删除。

OID

data_oid
Strict Mode严格模式mongo Shell Mode
{ "$oid": "<id>" }
ObjectId( "<id>" )

Where the values are as follows:其中值如下:

  • <id> is a 24-character hexadecimal string.是一个24个字符的十六进制字符串。

DB ReferenceDB参考

data_ref
Strict Mode严格模式mongo Shell Mode
{ "$ref": "<name>", "$id": "<id>" }
DBRef("<name>", "<id>")

Where the values are as follows:其中值如下:

  • <name> is a string of valid JSON characters.是一个有效的JSON字符字符串。
  • <id> is any valid extended JSON type.是任何有效的扩展JSON类型。

Undefined Type类型

data_undefined
Strict Mode严格模式mongo Shell Mode
{ "$undefined": true }
undefined

The representation for the JavaScript/BSON undefined type.JavaScript/BSON未定义类型的表示形式。

You cannot use undefined in query documents. 不能在查询文档中使用undefinedConsider the following document inserted into the people collection using the legacy mongo shell:考虑使用遗留的mongo shell将以下文档插入到people集合中:

db.people.insertOne( { name : "Sally", age : undefined } )

The following queries return an error:以下查询返回错误:

db.people.find( { age : undefined } )
db.people.find( { age : { $gte : undefined } } )

However, you can query for undefined values using $type, as in:但是,您可以使用$type查询未定义的值,如中所示:

db.people.find( { age : { $type : 6 } } )

This query returns all documents for which the age field has value undefined.此查询返回age字段值为undefined的所有文档。

Important

The undefined BSON type is deprecated. 不赞成使用未定义的BSON类型。mongosh stores a null value instead.会存储一个null值。

For example, use the same code to insert a document in mongosh and in the legacy mongo shell:例如,使用相同的代码在mongosh和遗留的mongo shell中插入文档:

db.people.insertOne( { name : "Sally", age : undefined } )

The resulting documents are different:生成的文档不同:

{ "name" : "Sally", "age" : null }
{ "name" : "Sally", "age" : undefined }

MinKey

data_minkey
Strict Mode严格模式mongo Shell Mode
{ "$minKey": 1 }
MinKey

The representation of the MinKey BSON data type that compares lower than all other types. MinKey BSON数据类型的表示形式,与所有其他类型相比更低。See Comparison/Sort Order for more information on comparison order for BSON types.有关BSON类型的比较顺序的更多信息,请参阅比较/排序顺序

MaxKey

data_maxkey
Strict Mode严格模式mongo Shell Mode
{ "$maxKey": 1 }
MaxKey

The representation of the MaxKey BSON data type that compares higher than all other types. MaxKey BSON数据类型的表示形式,与所有其他类型相比更高。See Comparison/Sort Order for more information on comparison order for BSON types.有关BSON类型的比较顺序的更多信息,请参阅比较/排序顺序

NumberLong

data_numberlong
Strict Mode严格模式mongo Shell Mode
{ "$numberLong": "<number>" }
NumberLong( "<number>" )

NumberLong is a 64 bit signed integer. 是一个64位有符号整数。In the legacy mongo shell, you must use quotation marks to insert a NumberLong or the operation will produce an error.在传统的mongoshell中,必须使用引号插入NumberLong,否则操作将产生错误。

For example, the following commands attempt to insert 9223372036854775807 as a NumberLong with and without quotation marks around the integer value:例如,以下命令尝试将9223372036854775807作为NumberLong插入,整数值周围有引号和没有引号:

db.json.insertOne( { longQuoted : NumberLong("9223372036854775807") } )
db.json.insertOne( { longUnQuoted : NumberLong(9223372036854775807) } )

The highlighted line produces an error in the legacy mongo shell. The insert succeeds in mongosh.突出显示的行在遗留的mongo shell中产生错误。插入在mongosh中成功。

NumberDecimal

data_numberdecimal
Strict Mode严格模式mongo Shell Mode
{ "$numberDecimal": "<number>" }
NumberDecimal( "<number>" )

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:例如,以下命令将123.40作为NumberDecimal插入,在值周围加引号和不加引号:

db.json.insertOne( { decimalQuoted : NumberDecimal("123.40") } )
db.json.insertOne( { decimalUnQuoted : NumberDecimal(123.40) } )

When you retrieve the documents, the value of decimalUnQuoted has changed, while decimalQuoted retains its specified precision:检索文档时,decimalUnQuoted的值已更改,而decimalQuoted保留其指定的精度:

db.json.find()
{ "_id" : ObjectId("596f88b7b613bb04f80a1ea9"), "decimalQuoted" : NumberDecimal("123.40") }
{ "_id" : ObjectId("596f88c9b613bb04f80a1eaa"), "decimalUnQuoted" : NumberDecimal("123.400000000000") }
Important

This insert behavior is different in mongosh.这种插入行为在mongosh中是不同的。

The quoted string format, NumberDecimal("123.40"), is deprecated. The insert succeeds, but also produces a warning.不赞成使用带引号的字符串格式NumberDecimal("123.40")。插入成功,但也会产生警告。

The unquoted string format, NumberDecimal(123.40), stores the value as 123.4. 未加引号的字符串格式NumberDecimal(123.40)将值存储为123.4The trailing 0 is dropped.尾部0已删除。