如何使用多个本地字段比较对mongoDB集合进行链式聚合?

如何使用多个本地字段比较对mongoDB集合进行链式聚合?,mongodb,nosql,aggregate,Mongodb,Nosql,Aggregate,以下是我的收藏: 用户: { user: xxx, job: xxx, order: xxxx } 汽车: 飞机: { order: xxxx, cost: yyyy } 保险: { cost: yyyy, terms: zzzz } 首先,我想根据“订单”字段的匹配值,将“用户”与“汽车”或“飞机”中的任何一个连接起来。之后,我将结果与基于“成本”字段的“保险”集合合并 所以,我希望结果是这样的: { "user" : xx, ..

以下是我的收藏:

用户:

{
    user: xxx,
    job: xxx,
    order: xxxx
}
汽车:

飞机:

{
    order: xxxx,
    cost: yyyy
}
保险:

{
    cost: yyyy,
    terms: zzzz
}
首先,我想根据“订单”字段的匹配值,将“用户”与“汽车”或“飞机”中的任何一个连接起来。之后,我将结果与基于“成本”字段的“保险”集合合并

所以,我希望结果是这样的:

{ "user" : xx, ..., "order": dsad, "cars": {}, "plane": {"order": ddsa, "cost": awew} , "terms": sdada},
{ "user" : yy, ..., "order": sawe, "cars": {"order": fda, "cost": qwez}, "plane": {} , "terms": tyrw},
{ "user" : zz, ..., "order": qwez, "cars": {}, "plane": {} , "terms":{} }
我尝试使用db.users.aggregate()和$lookup,如下所示:

db.check.aggregate([
{
    $lookup:
        {
            from: "cars",
            localField: "order",
            foreignField: "order",
            as: "cars"
        }    
},
{
    $unwind: {path: "$cars", preserveNullAndEmptyArrays: true}
},    
{
    $lookup:
        {
            from: "planes",
            localField: "order",
            foreignField: "order",
            as: "planes"
        }    
},
{
    $unwind: {path: "$planes", preserveNullAndEmptyArrays: true}
},  
其结果是:

{ "user" : xx, ..., "order": dsad, "cars": {}, "plane": {"order": ddsa, "cost": awew}},
{ "user" : yy, ..., "order": sawe, "cars": {"order": fda, "cost": qwez}, "plane": {}},
{ "user" : zz, ..., "order": qwez, "cars": {}, "plane": {}}
但是,我不知道从那里走得更远

你可以用它来得到你想要的东西

试试这个:

db.check.aggregate([
{
    $lookup:
        {
            from: "cars",
            localField: "order",
            foreignField: "order",
            as: "cars"
        }    
},
{
    $unwind: {path: "$cars", preserveNullAndEmptyArrays: true}
},    
{
    $lookup:
        {
            from: "planes",
            localField: "order",
            foreignField: "order",
            as: "planes"
        }    
},
{
    $unwind: {path: "$planes", preserveNullAndEmptyArrays: true}
},{
    $lookup:{
        from : "insurance",
        let  : {
            carCost : "$cars.cost",
            planeCost : "$planes.cost"
        },
        pipeline : [{
            $match : {
                $expr :{
                    $or : [{
                        cost : "$$carCost" 
                    },{
                        cost : "$$planeCost"
                    }
                    ]
                }
            }
        }],
        as : "insurance"
    }
},{
    $unwind: {path: "$insurance", preserveNullAndEmptyArrays: true}
},{
    $project : {
        user:1,
        order:1,
        cars:1,
        planes:1,
        terms : "$insurance.terms"
    }
}]
你可以用它来得到你想要的

试试这个:

db.check.aggregate([
{
    $lookup:
        {
            from: "cars",
            localField: "order",
            foreignField: "order",
            as: "cars"
        }    
},
{
    $unwind: {path: "$cars", preserveNullAndEmptyArrays: true}
},    
{
    $lookup:
        {
            from: "planes",
            localField: "order",
            foreignField: "order",
            as: "planes"
        }    
},
{
    $unwind: {path: "$planes", preserveNullAndEmptyArrays: true}
},{
    $lookup:{
        from : "insurance",
        let  : {
            carCost : "$cars.cost",
            planeCost : "$planes.cost"
        },
        pipeline : [{
            $match : {
                $expr :{
                    $or : [{
                        cost : "$$carCost" 
                    },{
                        cost : "$$planeCost"
                    }
                    ]
                }
            }
        }],
        as : "insurance"
    }
},{
    $unwind: {path: "$insurance", preserveNullAndEmptyArrays: true}
},{
    $project : {
        user:1,
        order:1,
        cars:1,
        planes:1,
        terms : "$insurance.terms"
    }
}]

您加入的匹配条件是什么?您还想做哪些您不能做的事情?@RaviShankarBharti首先,我想根据“订单”字段的匹配值,将“用户”加入“汽车”或“飞机”中。之后,我将结果与基于“成本”字段的“保险”集合合并。我不能做最后的加入(保险)基于比较“成本”的结果,无论是汽车或飞机!无论是汽车成本还是飞机成本都应该匹配,这就是你想要的吗?@RaviShankarBharti是的!!您加入的匹配条件是什么?您还想做哪些您不能做的事情?@RaviShankarBharti首先,我想根据“订单”字段的匹配值,将“用户”加入“汽车”或“飞机”中。之后,我将结果与基于“成本”字段的“保险”集合合并。我不能做最后的加入(保险)基于比较“成本”的结果,无论是汽车或飞机!无论是汽车成本还是飞机成本都应该匹配,这就是你想要的吗?@RaviShankarBharti是的!!你想要所有的条款还是只想要第一个?或者你想如何匹配?你想要所有的条款还是只想要第一个?或者你想如何匹配?