Overview
このガイドでは、 MongoDBドキュメントを操作するときに拡張JSONデータ形式を使用する方法を学習できます。
JSON は、オブジェクト、配列、数値、string、ブール値、null の値を表す人間が判読可能なデータ形式です。この形式は、 MongoDB がデータを保存するために使用する形式であるBSONデータ型のサブセットのみをサポートします。拡張JSON形式はより多くのBSONタイプをサポートしており、 BSONの各タイプに直接対応するフィールドタイプ情報を表すために "$" のプレフィックスが付いたキーの予約セットを定義します。
JSON、 BSON、 拡張JSONの詳細については、 JSONとBSONリソースおよび 拡張JSON MongoDB Server のマニュアル エントリを参照してください。
拡張 JSON 形式
MongoDB拡張JSON は、 BSONデータを表す string 形式を提供します。各形式は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. To specify this mode, pass bsoncxx::ExtendedJsonMode::k_canonical as a mode argument to the to_json() method. |
緩和 | 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. To specify this mode, pass bsoncxx::ExtendedJsonMode::k_relaxed as a mode argument to the to_json() method. |
Legacy | A string format that describes BSON documents with some type information loss. This format matches Relaxed Extended JSON with some exceptions. The C++ driver uses this mode by default. |
注意
C++ドライバーは、$uuid 拡張JSON型を string からバイナリ サブタイプ のb_binary 4オブジェクトに解析します。$uuid フィールド解析の詳細については、拡張JSON仕様の $uuid フィールドを解析するための特別なルール セクションを参照してください。
拡張 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 }
{ "_id": { "$oid": "573a1391f29313caabcd9637" }, "createdAt": { "$date": "1601499771648" }, "numViews": 36520312 }
拡張 JSON の読み取り
bsoncxx::from_json() メソッドを呼び出すと、拡張JSON string をC++ BSONドキュメントに読み込むことができます。このメソッドは、拡張JSON string を解析し、 データを含む bsoncxx::document::value を返します。
次の例は、 from_json() メソッドを使用して、拡張JSON string をBSONドキュメントに読み込む方法を示しています。
bsoncxx::document::value doc = bsoncxx::from_json(R"( { "_id": {"$oid": "507f1f77bcf86cd799439011"}, "myNumber": {"$numberLong": "4794261"} } )");
拡張 JSON の書込み (write)
拡張JSON string を書き込むには、bsoncxx::to_json() メソッドを使用します。デフォルトでは 、このメソッドはレガシー形式の拡張JSON string を返しますが、mode 引数を渡すことで、標準の または 形式を指定できます。
注意
レガシー バージョン
レガシー形式オプション は、 BSON型を Libbson レガシー拡張JSON形式で直列化するようにC++ドライバーに指示します。ドライバーはこれをデフォルトのモードとして使用します。
詳細については、 CドライバーAPIドキュメントの 「レガシー拡張JSON」ページを参照してください。
bsoncxx::to_json() メソッドは、array や document など、いくつかのコアおよび標準ライブラリ タイプで使用できます。次の例では、document 値を標準形式の拡張JSON string に変換します。
bsoncxx::builder::basic::document doc_builder; doc_builder.append(kvp("myNumber", 11223344)); doc_builder.append(kvp("myString", "String value")); bsoncxx::document::value doc = doc_builder.extract(); std::string json_str = bsoncxx::to_json(doc, bsoncxx::ExtendedJsonMode::k_canonical); std::cout << json_str << std::endl;
{"myNumber":{"$numberInt":"11223344"},"myString":"String value"}
詳細情報
API ドキュメント
このガイドで言及されている型とメソッドの詳細については、次のAPIドキュメントを参照してください。
サーバー マニュアル ページ
拡張JSONの詳細については、 MongoDB Serverマニュアルの「 MongoDB拡張JSON (v2 ) 」を参照してください。