我的架構看起來像這樣
批次收集:
{"_id":{"$oid":"61e82ed943389ffc5d277055"},
"name":"Batch ready to process",
"DocumentIDs":["61e82edb75d2841f2a8a023d"]}
檔案收集:
{"_id":{"$oid":"61e82edb75d2841f2a8a023d"},
"DocumentStatus":{"ActionId":"1","ActionType":"Type1","Status":"Status1"}
}
因此,批次集合具有檔案 ID 陣列。我如何合并這兩個集合?我試圖使用聚合查找,但它似乎不適用于這種情況,因為它的架構只允許比較單個簡單欄位,我需要檢查檔案 ID 是否存在于陣列“DocumentIDs”中。
因此,我只需要檔案集合中僅存在 2 個欄位的專案串列:“ActionType”和“Status”。我需要一個特定批次 ID 的串列 - 所以批次 ID 是一個過濾條件。
這是來自聚合查找的架構
/**
* from: The target collection.
* localField: The local join field.
* foreignField: The target join field.
* as: The name for the results.
* pipeline: The pipeline to run on the joined collection.
* let: Optional variables to use in the pipeline field stages.
*/
{
from: 'string',
localField: 'string',
foreignField: 'string',
as: 'string'
}
-雅切克
uj5u.com熱心網友回復:
可能是這樣的:
db.getCollection('batches').aggregate([
{$match: {_id : ObjectId('61e82ed943389ffc5d277055')}},
{$unwind : {"path" : "$DocumentIDs"}},
{$project: {
_id: 1,
"documentId" : {$toObjectId: "$DocumentIDs"}
}
},
{$lookup:
{
from: 'documents',
localField: "documentId",
foreignField: "_id",
as: "join_results"
}
},
{$unwind: {"path" : "$join_results"}},
{$project:
{
"_id" : 0,
"actionType" : "$join_results.DocumentStatus.ActionId",
"status" : "$join_results.DocumentStatus.Status",
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/417228.html
標籤:
下一篇:MongoDB聚合管道:條件乘法
