MongoDB Extended JSON (v1)
On this page本页内容
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中支持的数据类型,请参阅mongo
, see mongosh Data Types.mongosh
数据类型。
For supported data types in the legacy 有关旧版mongo shell中支持的数据类型,请参阅/core/shell-types。mongo
shell, see /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 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.任何JSON解析器都可以将这些严格的模式表示解析为键/值对;然而,只有MongoDB内部JSON解析器能够识别该格式所传递的类型信息。mongo
Shell mode.The MongoDB internal JSON parser and theMongoDB内部的JSON解析器和mongo
shell can parse this mode.mongo
shell可以解析这种模式。
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及更早版本各种MongoDB工具的--query
option 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.mongo
shell,可以将严格的模式表示解析为键/值对,但不需要识别类型信息。
Input in mongo
Shell Modemongo
Shell模式下的输入
mongo
Shell ModeThe following can parse representations in 以下内容可以在mongo
shell mode with recognition of the type information.mongo
shell模式下解析表示,并识别类型信息。
mongoimport
version 4.0 and earlier4.0及更早版本各种MongoDB工具的--query
option of various MongoDB tools--query
选项mongo
shell
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的Strict模式输出数据。
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 下面介绍了在Strict模式和mongo-Shell模式下的BSON数据类型和相关表示。mongo
Shell mode.
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 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表示,给出自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 (是一个字符串,该字符串可能包含有效的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由于JavaScript
andmongo Shell
representations support a limited range of options, any nonconforming options will be dropped when converting to this representation.JavaScript
和mongo
Shell表示支持的选项范围有限,因此在转换为此表示时,任何不一致的选项都将被删除。
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.undefined
。Consider the following document inserted into the考虑使用遗留的mongo shell将以下文档插入到people
collection using the legacymongo
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 valueundefined
.age
字段值为undefined
的所有文档。ImportantThe 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 legacymongo
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在传统的mongoshell中,必须使用引号插入mongo
shell, you must use quotation marks to insert aNumberLong
or the operation will produce an error.NumberLong
,否则操作将产生错误。For example, the following commands attempt to insert例如,以下命令尝试将9223372036854775807
as aNumberLong
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 inmongosh
.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 aNumberDecimal
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, whiledecimalQuoted
retains its specified precision:decimalUnQuoted
的值已更改,而decimalQuoted
保留其指定的精度:db.json.find()
{ "_id" : ObjectId("596f88b7b613bb04f80a1ea9"), "decimalQuoted" : NumberDecimal("123.40") }
{ "_id" : ObjectId("596f88c9b613bb04f80a1eaa"), "decimalUnQuoted" : NumberDecimal("123.400000000000") }ImportantThis 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 as123.4
.NumberDecimal(123.40)
将值存储为123.4
。The trailing尾部0
is dropped.0
已删除。