Docs Menu
Docs Home
/ /

확장 JSON 데이터로 작업하기

이 가이드 에서는 C 운전자 에서 확장 JSON 형식을 사용하는 방법을 학습 수 있습니다.

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

이러한 형식의 차이점에 대한 자세한 내용은 JSON 및 BSON 리소스 참조하세요.

MongoDB 확장 JSON BSON 데이터를 나타내는 다양한 문자열 형식을 제공합니다. 다양한 형식 각각은 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 유형을 문자열에서 하위 유형()의 바이너리 4 BSON_SUBTYPE_UUID데이터로 구문 분석합니다.$uuid 필드 분석에 대한 자세한 내용은 $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")
}
{
"_id": { "$oid": "573a1391f29313caabcd9637" },
"createdAt": { "$date": 1601499609 },
"numViews": { "$numberLong": "36520312" }
}

bson_new_from_json() 함수를 호출하여 확장 JSON 문자열을 BSON 문서 로 읽을 수 있습니다. 이 함수는 확장 JSON 문자열을 구문 분석하고 데이터가 포함된 bson_t 구조를 반환합니다.

다음 예시 예시 확장 JSON 문자열을 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 문자열을 구문 분석할 수도 있습니다. 이는 여러 JSON 문서를 처리 하거나 구문 분석 프로세스 더 잘 제어해야 할 때 특히 유용합니다.

다음 코드 예시 bson_json_reader_t 를 사용하여 확장 JSON 문자열을 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 문자열로 쓰기 (write) 수 있습니다. 이 함수는 bson_t 문서 출력 형식을 지정하는 옵션이 있는 확장 JSON 문자열로 변환합니다.

다음 예시 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_opts() API 설명서를 참조하세요.

확장 JSON 데이터로 작업하는 데 사용할 수 있는 함수 및 유형에 대해 자세히 학습 다음 API 설명서를 참조하세요.

돌아가기

BSON

이 페이지의 내용