【发布时间】:2020-05-26 15:26:51
【问题描述】:
现在我面临一个问题,我想根据变量加入 Mongo 中的集合,因为数据存储在不同的集合中。
我的数据集看起来像
{
_id:5e424331134cea013014ea9e0,
process:"Process1",
event:"Event1",
collection:"Collection1",
Date:"2020-02-11",
},
{
_id:5e424135151cea32114ea9e0,
process:"Process2",
event:"Event2",
collection:"Collection2",
Date:"2020-02-11",
}
我需要数据的集合保存在字段 $collection 的值中。
集合看起来像这样:
收藏1:
{
_id:5e63636636cea32114ea9e0,
item:81740147,
value:2,
Date:"2020-02-11",
}
集合2:
{
_id:5e63636636cea32114ea9e0,
user:testuser,
age:3,
Date:"2020-02-11",
}
我想在聚合管道中实现的是根据集合名称加入我的文档。
我已经尝试过的:
{
$lookup: {
from: '$collection',
localField: 'Date',
foreignField: 'Date',
as: 'join'
}
}
这对我不起作用。 也许你有不同的想法。
请记住,我只能使用聚合框架来解决问题,而不是mongodb的cli。
【问题讨论】:
-
您找到解决方案了吗?我也面临同样的困境
标签: mongodb variables mongoose aggregation-framework lookup