

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

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

Amazon DocumentDB の`$redact`集約ステージは、指定されたフィールドの値に基づいて出力ドキュメントにコンテンツを選択的に含めたり除外したりするために使用します。これは、アクセスレベルまたはユーザーのアクセス許可に基づいて機密データの可視性を制御する必要があるシナリオで特に役立ちます。

**パラメータ**
+ `$cond`: ドキュメント内の各フィールド`$$DESCEND`について、`$$KEEP`、`$$PRUNE`、または のいずれかに評価される式。
+ `$$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()
```

------