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中支持的数据类型,请参阅mongo, see mongosh Data Types.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格式中添加了以下扩展:
Strict mode严格模式. Strict mode representations of BSON types conform to the JSON RFC. 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.BSON类型的严格模式表示符合JSON RFC。任何JSON解析器都可以将这些严格模式表示解析为键/值对;然而,只有MongoDB内部JSON解析器能够识别格式所传达的类型信息。mongoShell mode. The MongoDB internal JSON parser and theMongoDB内部JSON解析器和mongoshell can parse this mode.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 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.以下内容可以在严格模式下解析表示,并识别类型信息。
mongoimportversion 4.0 and earlier4.0及更早版本各种MongoDB工具的--queryoption of various MongoDB tools--query选项- MongoDB Compass
Other JSON parsers, including 其他JSON解析器,包括mongo shell, can parse strict mode representations as key/value pairs, but without recognition of the type information.mongoshell,可以将严格模式表示解析为键/值对,但不识别类型信息。
Input in mongo Shell Modemongo Shell模式下的输入
mongo Shell ModeThe following can parse representations in 下面可以解析mongo shell mode with recognition of the type information.mongoshell模式下的表示,并识别类型信息。
mongoimportversion 4.0 and earlier4.0及更早版本各种MongoDB工具的--queryoption of various MongoDB tools--query选项mongoshell
Output in Strict Mode严格模式下的输出
Before version 4.2, 在4.2版本之前,mongoexport outputs data in strict mode of MongoDB Extended JSON v1.mongoexport以MongoDB Extended JSON v1的严格模式输出数据。
Output in mongo Shell Modemongo Shell模式下的输出
mongo Shell ModeBefore version 4.2, 在4.2版本之前,bsondump outputs in mongo shell mode.bsondump以mongo shell模式输出。
BSON Data Types and Associated RepresentationsBSON数据类型和相关表示
The following presents the BSON data types and the associated representations in strict mode and 下面介绍了严格模式和mongoshell模式下的BSON数据类型和相关表示。mongo shell mode.
Binary
data_binary-
Strict Mode严格模式mongoShell 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严格模式mongoShell 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>.<date>是ISO-8601日期格式,在模板YYYY-MM-DDTHH:mm:ss.mmm<+/-Offset>之后有一个必填时区字段。In Shell mode,在Shell模式下,<date>is the JSON representation of a 64-bit signed integer giving the number of milliseconds since epoch UTC.<date>是一个64位有符号整数的JSON表示,给出了自UTC纪元以来的毫秒数。
Timestamp
data_timestamp-
Strict Mode严格模式mongoShell 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严格模式mongoShell 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 (是一个字符串,可以包含有效的JSON字符和未转义的双引号(") 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).是一个只能包含字符“g”、“i”、“m”和“s”的字符串(在v1.9中添加)。Because the由于JavaScriptandmongo Shellrepresentations support a limited range of options, any nonconforming options will be dropped when converting to this representation.JavaScript和mongoShell表示支持的选项范围有限,因此在转换为此表示时,任何不符合要求的选项都将被删除。
OID
data_oid-
Strict Mode严格模式mongoShell 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严格模式mongoShell 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严格模式mongoShell Mode{ "$undefined": true }undefinedThe representation for the JavaScript/BSON undefined type.JavaScript/BSON未定义类型的表示。You cannot use不能在查询文档中使用undefinedin query documents. Consider the following document inserted into thepeoplecollection using the legacymongoshell:undefined。考虑使用传统mongoshell将以下文档插入到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此查询返回agefield has valueundefined.age字段值undefined的所有文档。Important
The undefined BSON type is deprecated.不推荐使用mongoshstores a null value instead.undefinedBSON类型。mongosh会存储一个null值代替它。For example, use the same code to insert a document in例如,使用相同的代码在mongoshand in the legacymongoshell:mongosh和遗留mongoshell中插入文档: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严格模式mongoShell Mode{ "$minKey": 1 }MinKeyThe 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严格模式mongoShell Mode{ "$maxKey": 1 }MaxKeyThe 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严格模式mongoShell Mode{ "$numberLong": "<number>" }Long( "<number>" )NumberLongis a 64 bit signed integer. In the legacymongoshell, you must use quotation marks to insert aNumberLongor the operation will produce an error.NumberLong是一个64位有符号整数。在传统的mongoshell中,必须使用引号插入NumberLong,否则操作将产生错误。For example, the following commands attempt to insert例如,以下命令尝试插入9223372036854775807as aNumberLongwith 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突出显示的行在遗留mongoshell. The insert succeeds inmongosh.mongoshell中产生错误。插入在mongosh中成功。
NumberDecimal
data_numberdecimal-
Strict Mode严格模式mongoShell Mode{ "$numberDecimal": "<number>" }Decimal128( "<number>" )NumberDecimalis 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.40as aNumberDecimalwith 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检索文档时,decimalUnQuotedhas changed, whiledecimalQuotedretains 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 as123.4.Decimal128.fromStringWithRounding("123.40")将值存储为123.4。The trailing尾随的0is dropped.0被丢弃。