[英]MongoDB extract subdocument array values from multiple documents
我正在使用 Bucket Pattern 来划分市场更新的时间序列并避免超出文档大小(传输开销不是问题)。 每个文档有 30,000 个更新,我需要从数组中匹配“marketId”的所有文档中返回这些更新(希望已排序)。
数据:
{
_id: 60617172eca858909eace71f,
marketId: '1.278363651',
eventId: 5697224,
marketType: 'OVER_UNDER_15',
size: 30000,
updates: [
{
t: 1616998770482.0,
p: 36.49
},
{
t: 1616998770482,
p: 87.77
},
// ... 29998 more
]
},
期望的结果:
[
{ t: 1616998770482, p: 36.49 },
{ t: 1616998770482, p: 16.59 },
{ t: 1616998770482, p: 40.38 },
... // sorted by t
}
这是我最接近聚合的尝试:
const result = await db.collection('markets').aggregate(
{ $match: { marketId: marketId } },
{ $project: { _id: 0, updates: 1 } },
{ $unwind: "$updates" },
{ $unwind: "$updates" },
).toArray();
Output:
[
{ updates: { t: 1616998770482, p: 36.49 } },
{ updates: { t: 1616998770482, p: 16.59 } },
{ updates: { t: 1616998770482, p: 40.38 } },
... // actually gives me all of them
}
如何删除“更新”并获得实际的 object?
演示 - https://mongoplayground.net/p/Dw7NAn9EoEd
db.collection.aggregate([
{ $match: { marketId: marketId } },
{ $project: { _id: 0, updates: 1 }},
{ $unwind: "$updates" },
{ $replaceRoot: { newRoot: "$updates" } }
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.