Docs Menu
Docs Home
/ / /
C#/.NET ドライバー
/

拡張 JSON

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

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

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

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.

緩和

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. The .NET/C# Driver uses Relaxed mode by default.

Shell

A string format 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.

注意

.NET/ C#ドライバーは、$uuid 拡張JSON型を string からバイナリ サブタイプ のBsonBinary 4オブジェクトに解析します。$uuid フィールド解析の詳細については、拡張JSON仕様の $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")
}

BsonDocument.Parse() メソッドを使用して、拡張JSONドキュメントをC#オブジェクトに読み込むことができます。次の例では、拡張JSONドキュメントをBsonDocumentオブジェクトに読み込みます。

var ejson = "{\n\"_id\": { \"$oid\": \"573a1391f29313caabcd9637\" },\n \"createdAt\": { \"$date\": { \"$numberLong\": \"1601499609\" }},\n\"numViews\": { \"$numberLong\": \"36520312\" }\n}\n\n";
var document = BsonDocument.Parse(ejson);
Console.WriteLine(document.ToJson());
{ "_id" : { "$oid" : "573a1391f29313caabcd9637" }, "createdAt" : { "$date" : "1970-01-19T12:51:39.609Z" }, "numViews" : 36520312 }

拡張JSON string は、BsonDocumentオブジェクトまたはカスタムクラスで ToJson() メソッドを呼び出すことで作成できます。パラメータとして、OutputModeプロパティが目的の拡張JSON形式に設定されている JsonWriterSettingsオブジェクトを指定する必要があります。

次のカスタムクラスについて考えてみます。

public class MyDocument
{
public ObjectId Id { get; set; }
public DateTime CreatedAt { get; set; }
public long NumViews { get; set; }
}

次の例では、CanonicalExtendedJson 値を OutputModeプロパティとして指定して、拡張JSON形式で MyDocument のインスタンスを出力します。

var document = new MyDocument();
document.Id = ObjectId.GenerateNewId();
document.CreatedAt = DateTime.UtcNow;
document.NumViews = 1234567890;
var json = document.ToJson(new JsonWriterSettings
{
OutputMode = JsonOutputMode.CanonicalExtendedJson
});
Console.WriteLine(json);
{ "_id" : { "$oid" : "68094769744af81f368ff1c1" }, "CreatedAt" : { "$date" : { "$numberLong" : "1745438569994" } }, "NumViews" : { "$numberLong" : "1234567890" } }

JSONドキュメントを操作するために使用できるメソッドとクラスの詳細については、次のAPIドキュメントを参照してください。

戻る

BSON

項目一覧