Docs 菜单
Docs 主页
/ / /
Ruby 驱动程序
/

扩展 JSON

在本指南中,您可以学习;了解在与MongoDB文档交互时如何使用扩展JSON数据格式。

JSON是一种人类可读的数据格式,用于表示对象值、数组、数字、字符串、布尔值和空值。此格式仅支持BSON数据类型的子集,而MongoDB正是使用该格式来存储数据。扩展JSON格式支持更多BSON 类型,定义了一设立以 "$" 为前缀的保留键,用于表示直接对应于BSON中每种类型的字段类型信息。

要学习;了解有关JSON、 BSON和扩展JSON的更多信息,请参阅 JSON和BSON资源以及扩展JSON MongoDB Server手册条目。

MongoDB扩展JSON提供字符串格式来表示BSON数据。 每种格式都符合JSON RFC 并满足特定使用案例。

下表描述了每种扩展JSON格式:

名称
说明

扩展规范

A string format that avoids loss of BSON type information during data conversions.
This format prioritizes type preservation at the loss of human-readability and interoperability with older formats.

宽松

A string format that describes BSON documents with some type information loss.
This format prioritizes human-readability and interoperability at the loss of certain type information.

Shell

A string format that matches the syntax used in the MongoDB shell.
This format prioritizes compatibility with the MongoDB shell, which often uses JavaScript functions to represent types.

注意

Ruby驾驶员将 $uuid 扩展JSON类型从字符串解析为二进制子类型 4 的 BsonBinary对象。 有关 $uuid字段解析的更多信息,请参阅扩展JSON规范中解析 $uuid 字段的特殊规则部分。

以下示例显示了包含对象标识符、日期和长数字字段的文档,这些字段分别以扩展 JSON 格式表示。单击与要查看的示例格式相对应的选项卡:

{
"_id": { "$oid": "573a1391f29313caabcd9637" },
"createdAt": { "$date": { "$numberLong": "1601499609" }},
"numViews": { "$numberLong": "36520312" }
}
{
"_id": { "$oid": "573a1391f29313caabcd9637" },
"createdAt": { "$date": "2020-09-30T18:22:51.648Z" },
"numViews": 36520312
}
{
"_id": ObjectId("573a1391f29313caabcd9637"),
"createdAt": ISODate("2020-09-30T18:22:51.648Z"),
"numViews": NumberLong("36520312")
}

您可以通过调用 BSON::ExtJSON.parse 方法将扩展JSON字符串读入Ruby大量。 此方法解析扩展JSON字符串并返回包含数据的大量。

以下示例展示如何使用 parse 方法将扩展JSON字符串读入哈希大量:

require 'bson'
ex_json = '''[
{"foo": [1, 2]},
{"bar": {"hello": "world"}},
{"code": {
"$scope": {},
"$code": "function x() { return 1; }"
}},
{"bin": {
"$type": "80",
"$binary": "AQIDBA=="
}}
]'''
doc = BSON::ExtJSON.parse(ex_json)
puts doc.class
puts doc
{"foo" => [1, 2]}
{"bar" => {"hello" => "world"}}
{"code" => #<BSON::CodeWithScope:0x0000000123f398e0 @javascript="function x() { return 1; }", @scope={}>}
{"bin" => <BSON::Binary:0x7144 type=user data=0x01020304...>}

您可以使用 as_extended_json 方法写入扩展JSON字符串。 默认下,此方法返回规范格式的扩展JSON字符串,但您可以通过传递 mode 参数来指定宽松或传统格式。

注意

旧版本

传统格式选项指示Ruby驾驶员使用MongoDB扩展JSON v1 格式序列化BSON 类型,该格式早于当前的宽松和规范格式。

有关详细信息,请参阅服务器手册中的MongoDB扩展JSON v1 页面。

as_extended_json 方法可用于多种核心和标准库类型,包括 ArrayHash。以下示例从哈希大量创建规范、宽松和传统格式的扩展JSON字符串:

require 'bson'
hash_array = [
{ "foo" => [1, 2] },
{ "bin" => BSON::Binary.new("\x01\x02\x03\x04", :user) },
{ "number" => BSON::Int64.new(42) }
]
json_string_canonical = hash_array.as_extended_json
json_string_relaxed = hash_array.as_extended_json(mode: :relaxed)
json_string_legacy = hash_array.as_extended_json(mode: :legacy)
puts "canonical:\t #{json_string_canonical}"
puts "relaxed:\t #{json_string_relaxed}"
puts "legacy:\t\t #{json_string_legacy}"
canonical: [{"foo":[{"$numberInt":"1"},{"$numberInt":"2"}]},{"bin":{"$binary":{"base64":"AQIDBA==","subType":"80"}}},{"number":{"$numberLong":"42"}}]
relaxed: [{"foo":[1,2]},{"bin":{"$binary":{"base64":"AQIDBA==","subType":"80"}}},{"number":42}]
legacy: [{"foo":[1,2]},{"bin":{"$binary":"AQIDBA==","$type":"80"}},{"number":42}]

有关更多信息,请参阅以下资源:

后退

BSON

在此页面上