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

확장 JSON 데이터로 작업하기

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

MongoDB 확장 JSON BSON 데이터를 나타내는 다양한 문자열 형식을 제공합니다. 다양한 형식 각각은 JSON RFC를 준수하며 특정 사용 사례를 충족합니다. 표준 형식이라고도 하는 확장 형식은 정보 손실 없는 양방향 변환을 위해 모든 BSON 유형에 대한 특정 표현을 제공합니다. 완화 모드 형식은 더 간결하고 일반 JSON 에 가깝지만 숫자 필드의 특정 비트 너비와 같은 모든 유형 정보를 나타내지는 않습니다.

각 형식에 대한 설명을 보려면 다음 표를 참조하세요.

이름
설명

확장

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.

JSON, BSON 및 확장 JSON 에 대해 자세히 학습하려면 MongoDB Server 매뉴얼에서 JSON 및 BSON확장 JSON 에 대한 문서를 참조하세요.

다음 예제에서는 확장 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
}

EJSON.stringify() 메서드를 사용하여 BSON 문서 객체 에서 확장 JSON 문자열을 쓰기 (write) 수 있습니다.

다음 예시 확장 JSON 문자열을 완화된 형식으로 출력합니다.

import { Code, BSON } from 'mongodb';
const EJSON = BSON.EJSON;
const doc = {
foo: [1, 2],
bar: { hello: "world" },
code: new Code("function x() { return 1; }", {}),
date: new Date(2024, 6, 20, 10, 30, 0),
};
const ejsonStr = EJSON.stringify(doc);
console.log(ejsonStr);
{"foo":[1,2],"bar":{"hello":"world"},"code":{"$code":"function x() { return 1; }","$scope":{}},"date":{"$date":"2024-07-20T14:30:00Z"}}

기본값 으로 stringify() 메서드는 확장 JSON 문자열을 완화된 형식으로 반환합니다. 표준 형식을 지정하려면 relaxed 옵션을 false로 설정하다 .

다음 예시 확장 JSON 표준 형식으로 출력하는 방법을 보여줍니다.

import { Code, BSON } from 'mongodb';
const EJSON = BSON.EJSON;
const doc = {
foo: [1, 2],
bar: { hello: "world" },
code: new Code("function x() { return 1; }", {}),
date: new Date(2024, 6, 20, 10, 30, 0),
};
const ejsonStr = EJSON.stringify(doc, { relaxed: false });
print(ejsonStr)
{"foo":[{"$numberInt":"1"},{"$numberInt":"2"}],"bar":{"hello":"world"},"code":{"$code":"function x() { return 1; }","$scope":{}},"date":{"$date":{"$numberLong":"1721485800000"}}}

EJSON.parse() 메서드를 사용하여 확장 JSON 문자열을 문자열에 설명된 JavaScript 값 또는 객체 로 읽을 수 있습니다.

다음 예시 parse() 메서드를 사용하여 확장 JSON 문자열을 JavaScript 값 또는 객체 로 읽는 방법을 보여줍니다.

import { BSON } from 'mongodb';
const EJSON = BSON.EJSON;
const ejsonStr = `{
"foo": [
{ "$numberInt": "1" },
{ "$numberInt": "2" }
],
"bar": { "hello": "world" },
"code": {
"$code": "function x() { return 1; }",
"$scope": {}
},
"bin": { "$binary": { "base64": "AQIDBA==", "subType": "00" } }
}`;
const doc = EJSON.parse(ejsonStr);
console.log(doc);
{
foo: [ 1, 2 ],
bar: { hello: 'world' },
code: new Code('function x() { return 1; }', {}),
bin: Binary.createFromBase64('AQIDBA==', 0)
}

참고

The driver parses the $uuid Extended JSON type from a string to a BsonBinary object of binary subtype 4. For more information about $uuid field parsing, see the Special Rules for Parsing $uuid Fields section in the extended JSON specification.

To learn more about any of the methods or types discussed in this guide, see the EJSON API documentation.

돌아가기

BSON

이 페이지의 내용