Node.js 在引用具有不同本地字段的同一表的聚合中使用两个“lookup Stage”
是否可以在Node.js 在引用具有不同本地字段的同一表的聚合中使用两个“lookup Stage”,node.js,mongodb,mongoose,aggregation-framework,Node.js,Mongodb,Mongoose,Aggregation Framework,是否可以在单个聚合查询中使用两个查找阶段,引用同一个表,但使用不同的本地字段 这是我的密码 const aggregate = [ { $match: { deleted: false, }, }, { $lookup: { from: 'users', localField: 'user',
单个聚合查询中使用两个查找阶段
,引用同一个表
,但使用不同的本地字段
这是我的密码
const aggregate = [
{
$match: {
deleted: false,
},
},
{
$lookup: {
from: 'users',
localField: 'user',
foreignField: '_id',
as: 'users',
},
},
{
$lookup: {
from: 'products',
localField: 'product',
foreignField: '_id',
as: 'products',
},
},
{
$lookup: {
from: 'products',
localField: 'otherProduct',
foreignField: '_id',
as: 'otherProducts',
},
},
{
$project: {
_id: 1,
users: 1,
products: 1,
otherProducts: 1
},
},
];
这是我使用的两个查找
{
$lookup: {
from: 'products',
localField: 'product',
foreignField: '_id',
as: 'products',
},
},
{
$lookup: {
from: 'products',
localField: 'otherProduct',
foreignField: '_id',
as: 'otherProducts',
},
}
我从第二个其他产品查找中得到空响应
但是,如果第一个产品查找($lookup:{from:'products')
被删除或注释,则第二个将给出数据
但是,如果我们同时添加两个查找,则第二个查找为空
不,因为我使用的版本不支持第一个答案中使用的函数,我尝试了第二个答案,但运气不好。你能显示你的数据库架构吗?你的查询是正确的,但我认为你的数据库中没有满足lookupFor MongoDB早于3.6的数据。这是不可能的