기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$$KEEP
$$KEEP 시스템 변수는 집계 파이프라인의 $redact 스테이지와 함께 사용되어 현재 문서 또는 필드를 변경하지 않고 출력에 포함합니다.
파라미터
없음
예제(MongoDB 쉘)
다음 예제에서는 Amazon DocumentDB 집계 파이프라인$$KEEP에서의 사용을 보여줍니다. 문서는 액세스가 "퍼블릭"과 같을 때만 보관되며, 그렇지 않으면 제거됩니다.
샘플 문서 생성
db.articles.insertMany([
{ title: "Article A", access: "public", content: "Visible content" },
{ title: "Article B", access: "private", content: "Hidden content" }
]);
쿼리 예제
db.articles.aggregate([
{
$redact: {
$cond: [
{ $eq: ["$access", "public"] },
"$$KEEP",
"$$PRUNE"
]
}
}
]);
출력
[
{
"_id" : ObjectId("..."),
"title" : "Article A",
"access" : "public",
"content" : "Visible content"
}
]
코드 예제
$$KEEP 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.
- Node.js
-
const { MongoClient } = require('mongodb');
async function run() {
const client = new MongoClient(
'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0'
);
try {
await client.connect();
const db = client.db('test');
const articles = db.collection('articles');
const pipeline = [
{
$redact: {
$cond: [
{ $eq: ["$access", "public"] },
"$$KEEP",
"$$PRUNE"
]
}
}
];
const results = await articles.aggregate(pipeline).toArray();
console.log(results);
} finally {
await client.close();
}
}
run().catch(console.error);
- Python
-
from pymongo import MongoClient
client = MongoClient(
"mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0"
)
db = client.test
articles = db.articles
pipeline = [
{
"$redact": {
"$cond": [
{"$eq": ["$access", "public"]},
"$$KEEP",
"$$PRUNE"
]
}
}
]
results = list(articles.aggregate(pipeline))
print(results)
client.close()