[英]MongoDB lookup and unwind
我正在嘗試執行一個工作正常的查找,並在正確的文檔中作為“指標”。 查找文檔在其 object 中有一個名為“history”的數組。 我正在嘗試展開該歷史記錄並對其執行一個方面,這是我直接在查找集合上運行的聚合查詢,效果很好。
但是,在這里使用它時,它不會返回任何東西。 我是否錯誤地展開了這個? 應該是$metrics.history
嗎?
{
from: 'historicprices',
localField: 'collectibleId',
foreignField: 'collectibleId',
pipeline: [
{$set: {"target-date": "$$NOW"}},
{$unwind: {path: "$history"}},
{$facet: {
"one_day": [
{ $match: { $expr: { $lte: [{$subtract: ["$target-date", "$history.date" ]}, {$multiply: [24,60,60,1000] }] } } },
{ $group: { _id: null, "first": { $first: "$history.value" }, "last": { $last: "$history.value" }, "min-price": {"$min": "$history.value"}, "max-price": {"$max": "$history.value"} } },
{ $unset: ["_id"]}
]
"one_week": [
{ $match: { $expr: { $lte: [{$subtract: ["$target-date", "$history.date" ]}, {$multiply: [7, 24, 60, 60, 1000] }] } } },
{ $group: { _id: null, "min-price": {"$min": "$history.value"}, "first": { $first: "$history.value" }, "last": { $last: "$history.value" }, "max-price": {"$max": "$history.value"} } },
{ $unset: ["_id"]}
]
}}
],
as: 'metrics',
}
謝謝
也許您需要將preserveNullAndEmptyArrays: true
添加到您的展開階段。 否則,如果其中一個文檔不返回此字段,則不會收到任何數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.