AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$redact(集計ステージ)

$redact

ドキュメント自体に保存されている情報に基づいて、ドキュメント全体またはドキュメント内のコンテンツの出力を制限します。

ミドルウェアと編集機能を備えたセキュリティ アーキテクチャの図。

The $redact stage has the following prototype form:

{ $redact: <expression> }

引数は、 $$DESCEND$$PRUNE 、または$$KEEPシステム変数に解決される限り、任意の有効なにすることができます。 式の詳細については、「式 」を参照してください。

システム変数
説明

$redact returns the fields at the current document level, excluding embedded documents. To include embedded documents and embedded documents within arrays, apply the $cond expression to the embedded documents to determine access for these embedded documents.

$redact excludes all fields at this current document/embedded document level, without further inspection of any of the excluded fields. This applies even if the excluded field contains embedded documents that may have different access levels.

$redact returns or keeps all fields at this current document/embedded document level, without further inspection of the fields at this level. This applies even if the included field contains embedded documents that may have different access levels.

このセクションの例では、db.collection.aggregate() ヘルパーを使用します。

forecasts コレクションには、次の形式のドキュメントが含まれます。ここで、tags フィールドには、そのドキュメントやめ込みドキュメント レベルのさまざまなアクセス値がリストされます。つまり、値 [ "G", "STLW" ] は、"G" または "STLW" のいずれかがデータにアクセスできることを指定します。

db.forecasts.insertOne(
{
_id: 1,
title: "123 Department Report",
tags: [ "G", "STLW" ],
year: 2014,
subsections: [
{
subtitle: "Section 1: Overview",
tags: [ "SI", "G" ],
content: "Section 1: This is the content of section 1."
},
{
subtitle: "Section 2: Analysis",
tags: [ "STLW" ],
content: "Section 2: This is the content of section 2."
},
{
subtitle: "Section 3: Budgeting",
tags: [ "TK" ],
content: {
text: "Section 3: This is the content of section 3.",
tags: [ "HCS" ]
}
}
]
}
)

A user has access to view information with either the tag "STLW" or "G". To run a query on all documents with year 2014 for this user, include a $redact stage as in the following:

var userAccess = [ "STLW", "G" ];
db.forecasts.aggregate(
[
{ $match: { year: 2014 } },
{ $redact: {
$cond: {
if: { $gt: [ { $size: { $setIntersection: [ "$tags", userAccess ] } }, 0 ] },
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
]
);

集約操作により、次の「編集済み」ドキュメントが返されます。

{
_id: 1,
title: "123 Department Report",
tags: [ "G", "STLW" ],
year: 2014,
subsections: [
{
subtitle: "Section 1: Overview",
tags: [ "SI", "G" ],
content: "Section 1: This is the content of section 1."
},
{
subtitle: "Section 2: Analysis",
tags: [ "STLW" ],
content : "Section 2: This is the content of section 2."
}
]
}

コレクションaccountsには、次のドキュメントが含まれています。

db.accounts.insertOne(
{
_id: 1,
level: 1,
acct_id: "xyz123",
cc: {
level: 5,
type: "yy",
num: 000000000000,
exp_date: ISODate("2015-11-01T00:00:00.000Z"),
billing_addr: {
level: 5,
addr1: "123 ABC Street",
city: "Some City"
},
shipping_addr: [
{
level: 3,
addr1: "987 XYZ Ave",
city: "Some City"
},
{
level: 3,
addr1: "PO Box 0123",
city: "Some City"
}
]
},
status: "A"
}
)

このサンプルドキュメントでは、level フィールドによって、データの表示に必要なアクセス レベルが決まります。

To run a query on all documents with status A and exclude all fields contained in a document/embedded document at level 5, include a $redact stage that specifies the system variable "$$PRUNE" in the then field:

db.accounts.aggregate(
[
{ $match: { status: "A" } },
{
$redact: {
$cond: {
if: { $eq: [ "$level", 5 ] },
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}
]
);

The $redact stage evaluates the level field to determine access. If the level field equals 5, then exclude all fields at that level, even if the excluded field contains embedded documents that may have different level values, such as the shipping_addr field.

集約操作により、次の「編集済み」ドキュメントが返されます。

{
_id: 1,
level: 1,
acct_id: "xyz123",
status: "A"
}

The result set shows that the $redact stage excluded the field cc as a whole, including the shipping_addr field which contained embedded documents that had level field values equal to 3 and not 5.

Tip

同じデータに対する複数のアクセスの組み合わせを設定する手順については、フィールド レベルの編集を実装するを参照してください。

このページのNode.js の例では、Atlasサンプルデータセットsample_mflixデータベースを使用します。無料のMongoDB Atlas cluster を作成し、サンプルデータセットをロードする方法については、 MongoDB Node.jsドライバーのドキュメントの開始を参照してください。

MongoDB Node.jsドライバーを使用して $redact ステージを集計パイプラインに追加するには、パイプラインオブジェクトで $redact 演算子を使用します。

次の例では、imdb.ratingフィールドの値が 9 以上であるドキュメントを保持するパイプラインステージを作成します。ステージでは他のすべてのドキュメントが除外されます。次に、この例では集計パイプラインを実行します。

const pipeline = [
{
$redact: {
$cond: {
if: { $gte: ["$imdb.rating", 9] },
then: "$$KEEP",
else: "$$PRUNE"
}
}
}
];
const cursor = collection.aggregate(pipeline);
return cursor;

このガイドのシステム変数の詳細については、「集計変数」ページを参照してください。

このページを評価

項目一覧