Docs Menu
Docs Home
/ /

拡張JSONデータとの連携

このガイドでは、 Cドライバーで拡張JSON形式を使用する方法を学習できます。

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

これらの形式の違いの詳細については、 JSONとBSON のリソース を参照してください。

MongoDB拡張JSON は、 BSONデータを表すためのさまざまな string 形式を機能します。異なる形式はそれぞれJSON RFC に準拠し、特定のユースケースを満たしています。

次の表は、各形式について説明します。

名前
説明

拡張

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.

Shell

Deprecated. JSON representation 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.
The legacy API uses this format.

厳密

Deprecated. This representation is the legacy format that fully conforms to the JSON RFC, which allows any JSON parser to read the type information.
The legacy API uses this format.

注意

ドライバーは、$uuid 拡張JSONタイプを string からサブタイプ4 (BSON_SUBTYPE_UUID )のバイナリ データに解析します。$uuid フィールド解析の詳細については、 $uuid フィールドを解析するための特別なルール を参照してください。

これらの形式の詳細については、次のリソースを参照してください。

次の例は、それぞれの拡張 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": ObjectId("573a1391f29313caabcd9637"),
"createdAt": ISODate("2020-09-30T18:22:51.648Z"),
"numViews": NumberLong("36520312")
}
{
"_id": { "$oid": "573a1391f29313caabcd9637" },
"createdAt": { "$date": 1601499609 },
"numViews": { "$numberLong": "36520312" }
}

bson_new_from_json() 関数を呼び出すと、拡張JSON string をBSONドキュメントに読み込むことができます。この関数は、拡張JSON string を解析し、 データを含む bson_t 構造を返します。

次の例は、拡張JSON string の例を bson_tドキュメントに読み込む方法を示しています。

/* Read Extended JSON string to BSON */
const char *ejson_str = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"},"
"\"myNumber\": {\"$numberLong\": \"4794261\" }}";
bson_error_t error;
bson_t *doc = bson_new_from_json ((const uint8_t *)ejson_str, -1, &error);

詳細については、 bson_new_from_json() および bson_as_canonical_extended_json() APIドキュメント を参照してください。

また、 JSON をBSONに変換するためのストリーミングインターフェースを提供する bson_json_reader_t タイプを使用して、拡張JSON string を解析することもできます。これは、複数のJSONドキュメントを処理する場合や、解析プロセスをより詳細に制御する必要がある場合に特に便利です。

次のコード例は、 bson_json_reader_t を使用して拡張JSON string をBSONドキュメントに変換する方法を示しています。

const char *ejson_str = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"},"
"\"myNumber\": {\"$numberLong\": \"4794261\" }}";
bson_json_reader_t *reader;
bson_error_t error;
bson_t doc = BSON_INITIALIZER;
int ret;
reader = bson_json_data_reader_new (false, 0);
bson_json_data_reader_ingest (reader, (const uint8_t *)ejson_str, strlen (ejson_str));
ret = bson_json_reader_read (reader, &doc, &error);
if (ret > 0) {
bson_iter_t iter;
if (bson_iter_init (&iter, &doc)) {
while (bson_iter_next (&iter)) {
const char *key = bson_iter_key (&iter);
if (strcmp (key, "_id") == 0 && BSON_ITER_HOLDS_OID (&iter)) {
const bson_oid_t *oid = bson_iter_oid (&iter);
char oid_str[25];
bson_oid_to_string (oid, oid_str);
printf ("%s is type: ObjectId\n", oid_str);
}
if (strcmp (key, "myNumber") == 0 && BSON_ITER_HOLDS_INT64 (&iter)) {
int64_t number = bson_iter_int64 (&iter);
printf ("%ld is type: int64_t\n", (long)number);
}
}
}
} else if (ret < 0) {
printf ("Error: %s\n", error.message);
}
bson_json_reader_destroy (reader);
bson_destroy (&doc);
507f1f77bcf86cd799439011 is type: ObjectId
4794261 is type: int64_t

詳しくは、次のAPIドキュメントを参照してください。

bson_as_json_with_opts() 関数を呼び出すと、 BSONドキュメントを拡張JSON string として書き込むことができます。この関数は、出力形式を指定するためのオプションを使用して、bson_tドキュメントを拡張JSON string に変換します。

次の例は、 BSONドキュメントを作成し、 緩和形式の拡張JSONとして出力する方法を示しています。

bson_t *doc;
bson_oid_t oid;
bson_json_opts_t *opts;
char *json_str;
// Create a BSON document
doc = bson_new ();
bson_oid_init_from_string (&oid, "507f1f77bcf86cd799439012");
bson_append_oid (doc, "_id", -1, &oid);
bson_append_int32 (doc, "myNumber", -1, 11223344);
// Configure JSON output options for Relaxed mode
opts = bson_json_opts_new (BSON_JSON_MODE_RELAXED, BSON_MAX_LEN_UNLIMITED);
// Convert to Extended JSON
json_str = bson_as_json_with_opts (doc, NULL, opts);
printf ("%s\n", json_str);
// Cleanup
bson_free (json_str);
bson_json_opts_destroy (opts);
bson_destroy (doc);
{ "_id" : { "$oid" : "507f1f77bcf86cd799439012" }, "myNumber" : 11223344 }

詳細については、 bson_as_json_with_ops() APIドキュメント を参照してください。

拡張JSONデータを操作するために使用できる関数とタイプの詳細については、次のAPIドキュメントを参照してください。

戻る

BSON

項目一覧