Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Node.js ドライバー
/

拡張JSONデータとの連携

JSON は、オブジェクト、配列、数値、string、ブール値、null の値を表すデータ形式です。 JSON$BSON拡張 形式では、MongoDB がデータを保存するために使用する形式である の各型に直接対応するフィールド型情報を表すために、"{0 " というプレフィックスが付いたキーの予約セットが定義されます。

MongoDB拡張JSON は、 BSONデータを表すためのさまざまな string 形式を機能します。異なる形式はそれぞれJSON RFC に準拠し、特定のユースケースを満たしています。拡張形式(標準形式とも呼ばれます)は、情報を失うことなく双方向変換を行うために、すべてのBSON型に対して特定の表現を備えています。緩和モード形式は、より簡潔で通常のJSONに近いものですが、数値フィールドの特定のビット幅などのすべての型情報を表すものではありません。

各形式の説明については、次の表を参照してください。

名前
説明

拡張

Also known as the Canonical format, this JSON representation avoids loss of BSON type information.
This format prioritizes type preservation at the loss of human-readability and interoperability with older formats.

緩和モード

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

JSON、 BSON、 拡張JSONの詳細については、 MongoDB ServerマニュアルのJSONとBSONと拡張JSONに関する記事を参照してください。

次の例は、 拡張JSON形式で表された ObjectId、date、long 数値フィールドを含むドキュメントを示しています。表示する例の形式に対応するタブをクリックします。

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

EJSON.stringify() メソッドを使用して、 BSONドキュメントオブジェクトから拡張JSON string を書き込むことができます。

次の例では、拡張JSON string を Relaxed形式で出力します。

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"}}

デフォルトでは 、stringify() メソッドは、 緩和形式で拡張JSON string を返します。標準形式を指定するには、relaxed オプションを false に設定します。

次の例は、拡張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"}}}

拡張JSON string は、EJSON.parse() メソッドを使用して、string によって記述されるJavaScript値またはオブジェクトに読み込むことができます。

次の例は、parse() メソッドを使用して、拡張JSON string を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)
}

注意

ドライバーは、$uuid拡張JSONタイプを string からバイナリ サブタイプ 4 の BsonBinaryオブジェクトに解析します。$uuidフィールド解析の詳細については、拡張JSON仕様の「 $uuid フィールドを解析するための特別なルール 」セクションを参照してください。

このガイドで説明したメソッドやタイプの詳細については、EJSON APIドキュメント を参照してください。

戻る

BSON

項目一覧