Important
Disambiguation消歧
The following page discusses MongoDB Extended JSON v2. For discussion on the Legacy MongoDB Extended JSON v1, see MongoDB Extended JSON (v1).下一页讨论MongoDB扩展JSON v2。有关遗留MongoDB扩展JSON v1的讨论,请参阅MongoDB扩展JSON(v1)。
For supported data types in 有关mongosh, see mongosh Data Types.mongosh中支持的数据类型,请参阅mongosh数据类型。
JSON can only directly represent a subset of the types supported by BSON. To preserve type information, MongoDB adds the following extensions to the JSON format.只能直接表示BSON支持的类型的子集。为了保留类型信息,MongoDB在JSON格式中添加了以下扩展。
-
Canonical Mode规范模式A string format that emphasizes type preservation at the expense of readability and interoperability. That is, conversion from canonical to BSON will generally preserve type information except in certain specific cases.一种以牺牲可读性和互操作性为代价强调类型保留的字符串格式。也就是说,从规范到BSON的转换通常会保留类型信息,除非在某些特定情况下。
-
Relaxed Mode放松模式A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information.一种以牺牲类型保留为代价强调可读性和互操作性的字符串格式。也就是说,从宽松格式转换为BSON可能会丢失类型信息。
Both formats conform to the JSON RFC and can be parsed by the various MongoDB drivers and tools.这两种格式都符合JSON RFC,可以被各种MongoDB驱动程序和工具解析。
MongoDB Extended JSON v2 UsageMongoDB扩展JSON v2使用
Drivers驱动程序
The following drivers support Extended JSON v2.0:以下驱动程序支持Extended JSON v2.0:
|
|
|
Extended JSON Methods扩展JSON方法
MongoDB provides the following methods for Extended JSON:MongoDB为扩展JSON提供了以下方法:
serialize |
|
deserialize |
|
stringify |
|
parse |
|
For usage examples, see Extended JSON Object Conversions below.有关使用示例,请参阅下面的扩展JSON对象转换。
For additional details, see the documentation for:有关更多详细信息,请参阅以下文档:
MongoDB Database ToolsMongoDB数据库工具
Starting in version 4.2:从4.2版本开始:
bsondump | |
mongodump |
|
mongoexport |
|
mongoimport |
|
BSON Data Types and Associated RepresentationsBSON数据类型和相关表示
The following presents some common BSON data types and the associated representations in Canonical and Relaxed.下面介绍了一些常见的BSON数据类型以及Canonical和Relaxed中的相关表示。
The complete list is here.完整的名单在这里。
Array
| Canonical | Relaxed |
|---|---|
|
|
Where the array elements are as follows:其中数组元素如下:
<elements>Array elements use Extended JSON.数组元素使用扩展JSON。To specify an empty array, omit the content要指定空数组,请省略内容[ ].[ ]。
Binary
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
"<payload>"Base64 encoded (with padding as "=") payload string.Base64编码(填充为“=”)有效载荷字符串。
"<t>"A one- or two-character hex string that corresponds to a BSON binary subtype. See the extended bson documentation http://bsonspec.org/spec.html for subtypes available.与BSON二进制子类型对应的一个或两个字符的十六进制字符串。请参阅扩展的bson文档http://bsonspec.org/spec.html以了解于可用的亚型。
Date
For dates between years 1970 and 9999, inclusive:对于1970年至9999年(含)之间的日期:
| Canonical | Relaxed |
|---|---|
|
|
For dates before year 1970 or after year 9999:对于1970年之前或9999年之后的日期:
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
"<millis>"A 64-bit signed integer as string. The value represents milliseconds relative to the epoch.一个64位带符号整数,作为字符串。该值表示相对于纪元的毫秒数。
"<ISO-8601 Date/Time Format>"A date in ISO-8601 Internet Date/Time Format as string.ISO-8601互联网日期/时间格式的日期字符串。The date/time has a maximum time precision of milliseconds:日期/时间的最大时间精度为毫秒:Fractional seconds have exactly 3 decimal places if the fractional part is non-zero.如果分数部分为非零,则分数秒精确到小数点后3位。Otherwise, fractional seconds SHOULD be omitted if zero.否则,如果为零,则应省略小数秒。
Decimal128
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
"<number>"A high-precision decimal as a string.字符串形式的高精度十进制数。
Document
| Canonical | Relaxed |
|---|---|
|
|
Where the document contents are as follows:文件内容如下:
<content>Name:value pairs that use Extended JSON.名称:使用扩展JSON的值对。To specify an empty document, omit the content若要指定空文档,请省略内容{ }.{ }。
Double
For finite numbers:对于有限数:
| Canonical | Relaxed |
|---|---|
|
|
For infinite numbers or NAN:对于无限数或NAN:
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
"<decimal string>"A 64-bit signed floating point as a string.字符串形式的64位有符号浮点。
<non-integer number>A non-integer number. Integer numbers are parsed as an integer instead of a double.非整数。整数被解析为整数而不是双精度数。
Int64
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
"<number>"A 64-bit signed integer as string.作为字符串的64位带符号整数。
<integer>A 64-bit signed integer.一个64位带符号整数。
Int32
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
"<number>"A 32-bit signed integer as a string.字符串形式的32位带符号整数。
<integer>A 32-bit signed integer.一个32位带符号整数。
MaxKey
| Canonical | Relaxed |
|---|---|
|
|
The MaxKey BSON data type compares higher than all other types. See Comparison/Sort Order for more information on comparison order for BSON types.MaxKey BSON数据类型比其他所有类型都高。有关BSON类型的比较顺序的更多信息,请参阅比较/排序顺序。
MinKey
| Canonical | Relaxed |
|---|---|
|
|
The MinKey BSON data type compares lower than all other types. See Comparison/Sort Order for more information on comparison order for BSON types.MinKey BSON数据类型比其他所有类型都低。有关BSON类型的比较顺序的更多信息,请参阅比较/排序顺序。
ObjectId
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
"<ObjectId bytes>"A 24-character, big-endian hexadecimal string that represents the ObjectId bytes.一个24个字符的大端十六进制字符串,表示ObjectId字节。
Regular Expression
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
"<regexPattern>"A string that corresponds to the regular expression pattern. The string can contain valid JSON characters and unescaped double quote (与正则表达式模式对应的字符串。字符串可以包含有效的JSON字符和未转义的双引号(") characters, but may not contain unescaped forward slash (/) characters.")字符,但不能包含未转义的正斜杠(/)字符。
"<options>"
Timestamp
| Canonical | Relaxed |
|---|---|
|
|
Where the values are as follows:其中值如下:
<t>A positive integer for the seconds since epoch.自纪元以来的秒数的正整数。
<i>A positive integer for the increment.增量为正整数。
Examples示例
The following examples illustrate Extended JSON usage.以下示例说明了扩展JSON的使用。
Type Representations类型表示
| "_id": | {"$oid":"5d505646cf6d4fe581014ab2"} | {"$oid":"5d505646cf6d4fe581014ab2"} |
| "arrayField": | ["hello",{"$numberInt":"10"}] | ["hello",10] |
| "dateField": | {"$date":{"$numberLong":"1565546054692"}} | {"$date":"2019-08-11T17:54:14.692Z"} |
| "dateBefore1970": | {"$date":{"$numberLong":"-1577923200000"}} | {"$date":{"$numberLong":"-1577923200000"}} |
| "decimal128Field": | {"$numberDecimal":"10.99"} | {"$numberDecimal":"10.99"} |
| "documentField": | {"a":"hello"} | {"a":"hello"} |
| "doubleField": | {"$numberDouble":"10.5"} | 10.5 |
| "infiniteNumber" | {"$numberDouble":"Infinity"} | {"$numberDouble":"Infinity"} |
| "int32field": | {"$numberInt":"10"} | 10 |
| "int64Field": | {"$numberLong":"50"} | 50 |
| "minKeyField": | {"$minKey":1} | {"$minKey":1} |
| "maxKeyField": | {"$maxKey":1} | {"$maxKey":1} |
| "regexField": | {"$regularExpression":{"pattern":"^H","options":"i"}} | {"$regularExpression":{"pattern":"^H","options":"i"}} |
| "timestampField": | {"$timestamp":{"t":1565545664,"i":1}} | {"$timestamp":{"t":1565545664,"i":1}} |
| "uuid": | {"$uuid":"3b241101-e2bb-4255-8caf-4136c566a962"} | {"$uuid":"3b241101-e2bb-4255-8caf-4136c566a962"} |
Extended JSON Object Conversions扩展JSON对象转换
The following short examples create a document object and then convert the object to different forms using Extended JSON object conversion methods.以下简短示例创建了一个文档对象,然后使用扩展的JSON对象转换方法将该对象转换为不同的形式。
Setup设置
Create a document in the 在conversions collection:conversions集合中创建文档:
db.conversions.insertOne( { insertDate: new Date() } )
mongosh returns a document object:返回一个文档对象:
{
acknowledged: true,
insertedId: ObjectId("61fbaf25671c45f3f5f4074a")
}EJSON.serialize
Serialize the data stored in a MongoDB document object:序列化存储在MongoDB文档对象中的数据:
serialized = EJSON.serialize( db.conversions.findOne() )
mongosh parses a JavaScript object and returns values using 解析JavaScript对象并使用前缀为"$" prefixed types:"$"的类型返回值:
{
_id: { '$oid': '61fbaf25671c45f3f5f4074a' },
insertDate: { '$date': '2022-02-03T10:32:05.230Z' }
}EJSON.deserialize
Deserialize a serialized object:反序列化序列化序列化对象:
EJSON.deserialize( serialized )
mongosh parses a JavaScript object and returns values using the default 解析JavaScript对象并使用默认的mongosh type form:mongosh类型形式返回值:
{
_id: new ObjectId( "61fbaf25671c45f3f5f4074a" ),
insertDate: ISODate( "2022-02-03T10:32:05.230Z" )
}EJSON.stringify
Convert an object to a string:将对象转换为字符串:
stringified = EJSON.stringify( db.conversions.findOne() )
mongosh outputs the elements of the converted object as strings:将转换后的对象的元素作为字符串输出:
{
"_id": {"$oid":"61fbaf25671c45f3f5f4074a"},
"insertDate":{"$date":"2022-02-03T10:32:05.230Z"}
}EJSON.parse
Parse a string to create an object:解析字符串以创建对象:
EJSON.parse( stringified )
mongosh returns the converted strings as documents:将转换后的字符串作为文档返回:
{
_id: new ObjectId("61fbaf25671c45f3f5f4074a"),
insertDate: ISODate("2022-02-03T10:32:05.230Z")
}