Docs Menu
Docs Home
/ / /
PHP ライブラリ マニュアル
/

拡張JSONデータとの連携

このガイドでは、 MongoDBドキュメントを操作するときに拡張JSONデータ形式を使用する方法を学習できます。

JSON は、オブジェクト、配列、数値、string、ブール値、null の値を表す人間が判読可能なデータ形式です。この形式は、 MongoDB がデータを保存するために使用する形式であるBSONデータ型のサブセットのみをサポートします。拡張JSON形式はより多くのBSONタイプをサポートしており、 BSONの各タイプに直接対応するフィールドタイプ情報を表すために "$" のプレフィックスが付いたキーの予約セットを定義します。

MongoDB拡張JSONには、 BSONデータを表す 2 つの文字列形式があります。 各形式は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.

JSON、 BSON、 拡張JSONの詳細については、JSONとBSONリソースおよび拡張JSON MongoDB Server のマニュアル エントリを参照してください。

次の例には、 拡張JSON形式で表された ObjectId、日付、long 数フィールドを含むドキュメントが示されています。それぞれの 拡張JSON形式でサンプルドキュメントを表示するには、 Canonical または Relaxed Modeタブを選択します。

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

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

次の例では、 BSONドキュメントを緩和 JSON 形式と標準拡張JSON形式の両方で出力します。

$doc = [
'foo' => [1, 2],
'bar' => ['hello' => 'world'],
'code' => new MongoDB\BSON\Javascript('function x() { return 1; }', []),
'date' => new DateTime('2024-07-20 10:30:00'),
];
echo 'Relaxed format: ' , MongoDB\BSON\Document::fromPHP($doc)->toRelaxedExtendedJSON(), PHP_EOL;
echo 'Canonical format: ' , MongoDB\BSON\Document::fromPHP($doc)->toCanonicalExtendedJSON(), PHP_EOL;
Relaxed format: { "foo" : [ 1, 2 ], "bar" : { "hello" : "world" }, "code" :
{ "$code" : "function x() { return 1; }", "$scope" : { } }, "date" : { } }
Canonical format: { "foo" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ],
"bar" : { "hello" : "world" }, "code" : { "$code" : "function x() { return 1; }",
"$scope" : { } }, "date" : { } }

拡張JSON string をPHP値に読み込むには、拡張JSON をPHP配列またはオブジェクトに変換する json_decode() メソッドを呼び出します。次の引数を json_decode() に渡します。

  • 読み取る拡張JSON string。

  • 配列値を返すかどうかを示すブール値。false に設定されている場合、メソッドはオブジェクト値を返します。

次の例では、拡張JSON string の値をPHP配列に変換します。

$ejsonStr = '{
"foo": [
{ "$numberInt": "1" },
{ "$numberInt": "2" }
],
"bar": { "hello": "world" },
"code": {
"$code": "function x() { return 1; }",
"$scope": {}
},
"bin": { "$binary": { "base64": "AQIDBA==", "subType": "00" } }
}';
$decodedJson = json_decode($ejsonStr, true);
print_r($decodedJson);
Array
(
[foo] => Array
(
[0] => Array
(
[$numberInt] => 1
)
[1] => Array
(
[$numberInt] => 2
)
)
[bar] => Array
(
[hello] => world
)
[code] => Array
(
[$code] => function x() { return 1; }
[$scope] => Array
(
)
)
[bin] => Array
(
[$binary] => Array
(
[base64] => AQIDBA==
[subType] => 00
)
)
)

このページで説明するメソッドの詳細については、次のPHP拡張APIドキュメントを参照してください。

戻る

時系列

項目一覧