Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
C#/ .NET 드라이버
/

확장 JSON

이 가이드 에서는 MongoDB 문서와 상호 작용할 때 확장 JSON 데이터 형식을 사용하는 방법을 학습 수 있습니다.

JSON 객체, 배열, 숫자, 문자열, 부울 및 null 값을 나타내는 사람이 읽을 수 있는 데이터 형식입니다. 이 형식은 MongoDB 데이터를 저장 데 사용하는 형식인 BSON 데이터 유형의 하위 집합만 지원합니다. 확장 JSON 형식은 더 많은 BSON 유형을 지원하며, BSON 의 각 유형에 직접적으로 대응하는 필드 유형 정보를 나타내기 위해 '$' 접두사가 붙은 예약된 키 설정하다 정의합니다.

JSON, BSON 및 확장 JSON 에 대해 자세히 학습 JSON 및 BSON 리소스 및 확장 JSON MongoDB Server 수동 항목을 참조하세요.

MongoDB 확장 JSON BSON 데이터를 나타내는 문자열 형식을 제공합니다. 각 형식은 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 유형을 바이너리 하위 유형 4의 BsonBinary 객체 로 구문 분석합니다. $uuid 필드 분석에 대한 자세한 내용은 확장 JSON 사양의 $uuid 필드 구문 분석을 위한 특별 규칙 섹션을 참조하세요.

다음 예시에서는 각 확장 JSON 형식으로 표시되는 ObjectId, 날짜, 긴 숫자 필드가 포함된 문서를 보여 줍니다. 원하는 형식의 예시 탭을 클릭합니다.

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

BsonDocument 객체 또는 사용자 지정 클래스에서 ToJson() 메서드를 호출하여 확장 JSON 문자열을 쓰기 (write) 수 있습니다. 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

이 페이지의 내용