정의
$redact문서 자체에 저장된 정보를 기반으로 전체 문서 또는 문서 내 컨텐츠가 출력되지 않도록 제한합니다.
The
$redactstage has the following prototype form:{ $redact: <expression> } 인수는
$$DESCEND,$$PRUNE또는$$KEEP시스템 변수로 해석되는 한 유효한 표현식 이 될 수 있습니다. 표현식에 대한 자세한 내용은 표현식을 참조하세요 .시스템 변수설명$redactexcludes 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.$redactreturns 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 collection에는 다음 형식의 문서가 포함되어 있습니다. 여기서 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 필드는 데이터를 보는 데 필요한 액세스 수준을 결정합니다.
상태가 A 인 모든 문서에 대해 쿼리를 실행하고 5 수준의 문서/내장된 문서에 포함된 모든 필드를 제외하려면 then 필드에 시스템 변수 "$$PRUNE"을 지정하는 $redact 단계를 포함합니다.
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.
팁
필드 수준 편집을 구현하여 동일한 데이터에 대한 여러 가지 액세스 조합을 설정하는 단계를 수행합니다.
이 페이지의 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;
자세히 알아보기
이 가이드 의 시스템 변수에 대해 자세히 학습하려면 애그리게이션 변수 페이지를 참조하세요.