Docs Home / Node.js Driver

Work with Extended JSON Data使用扩展JSON数据

Overview概述

JSON is a data format that represents the values of objects, arrays, numbers, strings, booleans, and nulls. The Extended JSON format defines a reserved set of keys prefixed with "$" to represent field type information that directly corresponds to each type in BSON, the format that MongoDB uses to store data.JSON是一种数据格式,表示对象、数组、数字、字符串、布尔值和空值的值。扩展JSON格式定义了一组以“$”为前缀的保留键,用于表示直接对应于BSON中每种类型的字段类型信息,BSON是MongoDB用于存储数据的格式。

Extended JSON Formats扩展JSON格式

MongoDB Extended JSON features different string formats to represent BSON data. Each of the different formats conform to the JSON RFC and meet specific use cases. MongoDB Extended JSON具有不同的字符串格式来表示BSON数据。每种不同的格式都符合JSON RFC,并满足特定的用例。The Extended format, also known as the Canonical format, features specific representations for every BSON type for bidirectional conversion without loss of information. 扩展格式,也称为规范格式,为每种BSON类型提供特定的表示,以便在不丢失信息的情况下进行双向转换。The Relaxed Mode format is more concise and closer to ordinary JSON, but does not represent all the type information such as the specific bit width of numeric fields.松弛模式格式更简洁,更接近普通JSON,但并不表示所有类型信息,如数值字段的特定位宽。

See the following table to see a description of each format:请参阅下表,了解每种格式的说明:

Name名称Description描述
Extended扩展的Also known as the Canonical format, this JSON representation avoids loss of BSON type information.也称为规范格式,这种JSON表示避免了BSON类型信息的丢失。
This format prioritizes type preservation at the loss of human-readability and interoperability with older formats.这种格式优先考虑类型保留,而不考虑人类可读性和与旧格式的互操作性。
Relaxed Mode放松模式JSON representation that describes BSON documents with some type information loss.描述BSON文档的JSON表示,其中丢失了一些类型信息。
This format prioritizes human-readability and interoperability at the loss of certain type information.这种格式在丢失某些类型信息的情况下,优先考虑人类的可读性和互操作性。

To learn more about JSON, BSON, and Extended JSON, see our article about JSON and BSON and Extended JSON in the MongoDB Server manual.要了解有关JSON、BSON和扩展JSON的更多信息,请参阅我们在MongoDB服务器手册中关于JSON和BSON以及扩展JSON的文章。

Extended JSON Examples扩展JSON示例

The following examples show a document containing an ObjectId, date, and long number field represented in the Extended JSON format. Click the tab that corresponds to the format of the example you want to see:以下示例显示了一个包含以扩展JSON格式表示的ObjectId、日期和长数字字段的文档。单击与要查看的示例格式对应的选项卡:

Extended
{
"_id": { "$oid": "573a1391f29313caabcd9637" },
"createdAt": { "$date": { "$numberLong": "1601499609" }},
"numViews": { "$numberLong": "36520312" }
}
Relaxed Mode
{
"_id": { "$oid": "573a1391f29313caabcd9637" },
"createdAt": { "$date": "2020-09-30T18:22:51.648Z" },
"numViews": 36520312
}

Write Extended JSON写入扩展JSON

You can write an Extended JSON string from a BSON document object by using the EJSON.stringify() method.您可以使用EJSON.stringify()方法从BSON文档对象编写扩展JSON字符串。

The following example outputs an Extended JSON string in the Relaxed format:以下示例以Relaxed格式输出扩展JSON字符串:

import { Code, BSON } from 'mongodb';
const EJSON = BSON.EJSON;

const doc = {
foo: [1, 2],
bar: { hello: "world" },
code: new Code("function x() { return 1; }", {}),
date: new Date(2024, 6, 20, 10, 30, 0),
};

const ejsonStr = EJSON.stringify(doc);
console.log(ejsonStr);
{
"foo": [
1,
2
],
"bar": {
"hello": "world"
},
"code": {
"$code": "function x() { return 1; }",
"$scope": {}
},
"date": {
"$date": "2024-07-20T14:30:00Z"
}
}

By default, the stringify() method returns the Extended JSON string in the Relaxed format. To specify the Canonical format, set the relaxed option to false.默认情况下,stringify()方法以Relaxed格式返回扩展JSON字符串。要指定Canonical格式,请将relaxed选项设置为false

The following example shows how to output Extended JSON in the Canonical format:以下示例显示了如何以规范格式输出扩展JSON:

import { Code, BSON } from 'mongodb';
const EJSON = BSON.EJSON;

const doc = {
foo: [1, 2],
bar: { hello: "world" },
code: new Code("function x() { return 1; }", {}),
date: new Date(2024, 6, 20, 10, 30, 0),
};

const ejsonStr = EJSON.stringify(doc, { relaxed: false });
print(ejsonStr)
{
"foo": [
{
"$numberInt": "1"
},
{
"$numberInt": "2"
}
],
"bar": {
"hello": "world"
},
"code": {
"$code": "function x() { return 1; }",
"$scope": {}
},
"date": {
"$date": {
"$numberLong": "1721485800000"
}
}
}

Read Extended JSON读取扩展JSON

You can read an Extended JSON string into the JavaScript value or object described by the string by using the EJSON.parse() method.您可以使用EJSON.parse()方法将扩展JSON字符串读取到字符串描述的JavaScript值或对象中。

The following example shows how you can read an Extended JSON string into a JavaScript value or object by using the parse() method:以下示例显示了如何使用parse()方法将扩展JSON字符串读入JavaScript值或对象:

import { BSON } from 'mongodb';
const EJSON = BSON.EJSON;

const ejsonStr = `{
"foo": [
{ "$numberInt": "1" },
{ "$numberInt": "2" }
],
"bar": { "hello": "world" },
"code": {
"$code": "function x() { return 1; }",
"$scope": {}
},
"bin": { "$binary": { "base64": "AQIDBA==", "subType": "00" } }
}`;

const doc = EJSON.parse(ejsonStr);
console.log(doc);
{
foo: [ 1, 2 ],
bar: { hello: 'world' },
code: new Code('function x() { return 1; }', {}),
bin: Binary.createFromBase64('AQIDBA==', 0)
}

Note

The driver parses the $uuid Extended JSON type from a string to a BsonBinary object of binary subtype 4. For more information about $uuid field parsing, see the Special Rules for Parsing $uuid Fields section in the extended JSON specification.驱动程序将$uuid Extended JSON类型从字符串解析为二进制子类型4的BsonBinary对象。有关$uuid字段解析的更多信息,请参阅扩展JSON规范中的解析$uuid域的特殊规则部分。

API Documentation文档

To learn more about any of the methods or types discussed in this guide, see the EJSON API documentation.要了解有关本指南中讨论的任何方法或类型的更多信息,请参阅EJSON API文档。