Database Manual / Introduction / BSON Types

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扩展JSON v1(遗留扩展JSON)。有关MongoDB扩展JSON v2的讨论,请参阅MongoDB扩展JSON(v2)

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

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格式中添加了以下扩展:

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 support Extended JSON v1.0 (Legacy):以下驱动程序支持扩展JSON v1.0(传统版):

  • C#
  • Ruby

Note

The C# Driver 3.0 and later does not have support for strict mode but supports shell mode.C# Driver 3.0及更高版本不支持严格模式,但支持shell模式。

For the other drivers, refer to MongoDB Extended JSON (v2).对于其他驱动程序,请参阅MongoDB扩展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的严格模式输出数据。

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

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

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

The following presents the BSON data types and the associated representations in strict mode and mongo shell mode.下面介绍了严格模式和mongoshell模式下的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. <t> 是指示数据类型的单个字节的表示。In Strict mode it is a hexadecimal string, and in Shell mode it is an integer. 在严格模式下,它是一个十六进制字符串,在Shell模式下,是一个整数。See the extended bson documentation. 请参阅扩展的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表示,给出了自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.是自纪元以来秒数的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 Reference引用

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. Consider the following document inserted into the people collection using the legacy mongo shell:不能在查询文档中使用undefined。考虑使用传统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. mongosh stores a null value instead.不推荐使用undefinedBSON类型。mongosh会存储一个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>" }
Long( "<number>" )

NumberLong is a 64 bit signed integer. In the legacy mongo shell, you must use quotation marks to insert a NumberLong or the operation will produce an error.NumberLong是一个64位有符号整数。在传统的mongo shell中,必须使用引号插入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 : Long("9223372036854775807") } )
db.json.insertOne( { longUnQuoted : Long(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>" }
Decimal128( "<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 : Decimal128("123.40") } )
db.json.insertOne( { decimalUnQuoted : Decimal128(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" : Decimal128("123.40") }
{ "_id" : ObjectId("596f88c9b613bb04f80a1eaa"), "decimalUnQuoted" : Decimal128("123.400000000000") }

Important

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

The unquoted string format, Decimal128(123.40), is deprecated. The insert succeeds, but also produces a warning.不推荐使用未加引号的字符串格式Decimal128(123.40)。插入成功,但也会产生警告。

The quoted string format, Decimal128.fromStringWithRounding("123.40") stores the value as 123.4. 引号字符串格式Decimal128.fromStringWithRounding("123.40")将值存储为123.4The trailing 0 is dropped.尾随的0被丢弃。