

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

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

Amazon DocumentDB의 `$redact` 집계 단계는 지정된 필드의 값을 기반으로 출력 문서에서 콘텐츠를 선택적으로 포함하거나 제외하는 데 사용됩니다. 이는 액세스 수준 또는 사용자 권한에 따라 민감한 데이터의 가시성을 제어해야 하는 시나리오에서 특히 유용합니다.

**파라미터**
+ `$cond`: 문서의 각 필드에 `$$DESCEND` 대해 `$$PRUNE`, 또는 `$$KEEP`로 평가되는 표현식입니다.
+ `$$KEEP`: 출력 문서에 현재 필드를 유지합니다.
+ `$$PRUNE`: 출력 문서에서 현재 필드를 제거합니다.
+ `$$DESCEND`: 객체 또는 배열인 현재 필드에 `$redact` 스테이지를 반복적으로 적용합니다.

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

이 예제에서는 `$redact` 스테이지를 사용하여 상태를 기준으로 주문을 필터링하고 특정 상태 값이 있는 주문만 표시합니다.

**샘플 문서 생성**

```
db.orders.insert([
  { "_id": 1, "status": "shipped", "customer": "Carlos Salazar", "total": 150.00, "date": "2025-01-15" },
  { "_id": 2, "status": "processing", "customer": "Saanvi Sarkar", "total": 89.99, "date": "2025-01-20" },
  { "_id": 3, "status": "cancelled", "customer": "Zhang Wei", "total": 220.50, "date": "2025-01-18" }
])
```

**쿼리 예제**

```
db.orders.aggregate([
  {
    $redact: {
      $cond: {
        if: { $in: ["$status", ["shipped", "processing"]] },
        then: "$$KEEP",
        else: "$$PRUNE"
      }
    }
  }
])
```

**출력**

```
[
  { _id: 1, status: 'shipped', customer: 'Carlos Salazar', total: 150, date: '2025-01-15' },
  { _id: 2, status: 'processing', customer: 'Saanvi Sarkar', total: 89.99, date: '2025-01-20' }
]
```

이 예제에서 `$redact` 단계는 각 문서의 `status` 필드 값을 확인합니다. 이 "배송" 또는 "처리"`status`인 경우 문서는 보관됩니다(`$$KEEP`). 그렇지 않으면 문서가 정리됩니다(`$$PRUNE`).

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

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

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

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

async function example() {
  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('orders');

  const result = await collection.aggregate([
    {
      $redact: {
        $cond: {
          if: { $in: ['$status', ['shipped', 'processing']] },
          then: '$$KEEP',
          else: '$$PRUNE'
        }
      }
    }
  ]).toArray();

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

example();
```

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

```
from pymongo import MongoClient

def example():
    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['orders']

    result = list(collection.aggregate([
        {
            '$redact': {
                '$cond': {
                    'if': { '$in': ['$status', ['shipped', 'processing']] },
                    'then': '$$KEEP',
                    'else': '$$PRUNE'
                }
            }
        }
    ]))

    print(result)
    client.close()

example()
```

------