

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# \$1jsonSchema
<a name="jsonSchema"></a>

버전 4.0의 새 버전입니다.

Elastic 클러스터에서는 지원되지 않습니다.

Amazon DocumentDB의 `$jsonSchema` 연산자는 지정된 JSON 스키마를 기반으로 문서를 필터링하는 데 사용됩니다. 이 연산자를 사용하면 특정 JSON 스키마와 일치하는 문서를 쿼리하여 검색된 문서가 특정 구조 및 데이터 유형 요구 사항을 준수하는지 확인할 수 있습니다.

`$jsonSchema` 평가 쿼리 연산자를 컬렉션 생성의 일부로 사용하여 컬렉션에 삽입되는 문서의 스키마를 검증할 수 있습니다. 추가 정보는 [JSON 스키마 검증 사용](json-schema-validation.md)을 참조하세요.

**파라미터**
+ `required` (배열): 문서의 필수 필드를 지정합니다.
+ `properties` (객체): 문서의 각 필드에 대한 데이터 유형 및 기타 제약 조건을 정의합니다.

## 예제(MongoDB 쉘)
<a name="jsonSchema-examples"></a>

다음 예제에서는 연`$jsonSchema`산자를 사용하여 `name`, `employeeId` 및 `age` 필드가 있고 `employeeId` 필드가 유형인 문서만 검색하도록 `employees` 컬렉션을 필터링하는 방법을 보여줍니다`string`.

**샘플 문서 생성**

```
db.employees.insertMany([
  { "name": { "firstName": "Carol", "lastName": "Smith" }, "employeeId": "1" },
  { "name": { "firstName": "Emily", "lastName": "Brown" }, "employeeId": "2", "age": 25 },
  { "name": { "firstName": "William", "lastName": "Taylor" }, "employeeId": 3, "age": 24 },
  { "name": { "firstName": "Jane", "lastName": "Doe" }, "employeeId": "4" }
]);
```

**쿼리 예제**

```
db.employees.aggregate([
  { $match: {
    $jsonSchema: {
      required: ["name", "employeeId", "age"],
      properties: { "employeeId": { "bsonType": "string" } }
    }
  }}
]);
```

**출력**

```
{ "_id" : ObjectId("6908e8b61f77fc26b2ecd26f"), "name" : { "firstName" : "Emily", "lastName" : "Brown" }, "employeeId" : "2", "age" : 25 }
```

## 코드 예제
<a name="jsonSchema-code"></a>

`$jsonSchema` 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

------
#### [ Node.js ]

```
const { MongoClient } = require('mongodb');

async function filterByJsonSchema() {
  const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
  const db = client.db('test');
  const collection = db.collection('employees');

  const result = await collection.aggregate([
    {
      $match: {
        $jsonSchema: {
          required: ['name', 'employeeId', 'age'],
          properties: { 'employeeId': { 'bsonType': 'string' } }
        }
      }
    }
  ]).toArray();

  console.log(result);
  await client.close();
}

filterByJsonSchema();
```

------
#### [ Python ]

```
from pymongo import MongoClient

def filter_by_json_schema():
  client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
  db = client['test']
  collection = db['employees']

  result = list(collection.aggregate([
    {
      '$match': {
        '$jsonSchema': {
          'required': ['name', 'employeeId', 'age'],
          'properties': {'employeeId': {'bsonType': 'string'}}
        }
      }
    }
  ]))

  print(result)
  client.close()

filter_by_json_schema()
```

------