本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$$PRUNE
$$PRUNE 系統變數會與彙總管道中的 $redact階段搭配使用,以從結果中排除文件或內嵌文件層級。當條件評估為 時$$PRUNE,目前的文件或子文件會從輸出中移除。它通常與 $$DESCEND(保留和周遊文件) 或 $$KEEP(保留文件的所有層級) 搭配使用。
參數
無. $$PRUNE 系統變數在沒有參數的情況下使用,且必須與 搭配使用$redact。
範例 (MongoDB Shell)
下列範例示範如何使用 $$PRUNE搭配 $redact,從結果中排除超過 30 年的使用者。
建立範例文件
db.users.insert([ { _id:1, name: "Carlos Salazar", age: 35, address: { street: "123 Main St", city: "Anytown", state: "CA" } }, { _id:2, name: "Saanvi Sarkar", age: 28, address: { street: "456 Oak Rd", city: "Someplace", state: "NY" } }, { _id:3, name: "Li Juan", age: 42, address: { street: "789 Pine Ave", city: "Springfield", state: "TX" } } ])
查詢範例
db.users.aggregate([ { $redact: { $cond: { if: { $gt: ["$age", 30] }, then: "$$PRUNE", else: "$$DESCEND" } } } ])
輸出
[
{
"_id": 2,
"name": "Saanvi Sarkar",
"age": 28,
"address": {
"street": "456 Oak Rd",
"city": "Someplace",
"state": "NY"
}
}
]
程式碼範例
若要檢視使用 $$PRUNE命令的程式碼範例,請選擇您要使用的語言標籤: