MongoDB中的完全外部连接

MongoDB中的完全外部连接,mongodb,join,outer-join,nosql,Mongodb,Join,Outer Join,Nosql,我想通过查找MongoDB查询在MongoDB中进行完整的外部联接。这可能吗?MongoDB支持的完全外部联接是否有其他替代方案 [更新:] 我希望获得Collection1和Collection2的结果,如下附件所示: 例如: 在上述结果列中,可能会有不同的算术运算,并将在计算中进一步使用 我将集合命名为coll1和coll2,然后使用此查询,它将为您提供所需的输出 db.getCollection('coll1').aggregate([ { $facet: {

我想通过查找MongoDB查询在MongoDB中进行完整的外部联接。这可能吗?MongoDB支持的完全外部联接是否有其他替代方案

[更新:]

我希望获得Collection1和Collection2的结果,如下附件所示:

例如:


在上述结果列中,可能会有不同的算术运算,并将在计算中进一步使用

我将集合命名为coll1和coll2,然后使用此查询,它将为您提供所需的输出

db.getCollection('coll1').aggregate([
    {
        $facet: {
            commonRecords: [{
                    $lookup: {
                        from: "coll2",
                        localField: 'name',
                        foreignField: 'name',
                        as: "coll2"
                    }
                },
                {
                    $unwind: {
                        path: '$coll2',
                        preserveNullAndEmptyArrays: true
                    }
                }
            ]
        }
    },
    {
        $lookup: {
            from: "coll2",
            let: {
                names: {
                    $map: {
                        input: '$commonRecords',
                        as: 'commonRecord',
                        in: '$$commonRecord.name'
                    }
                }
            },
            pipeline: [{
                $match: {
                    $expr: {
                        $eq: [{
                            $indexOfArray: ['$$names', '$name']
                        }, -1]
                    }
                }
            }, ],
            as: "coll2"
        }
    },
    {
        $addFields: {
            coll2: {
                $map: {
                    input: '$coll2',
                    as: 'doc',
                    in: {
                        coll2: '$$doc'
                    }
                }
            }
        }
    },
    {
        $project: {
            records: {
                $concatArrays: ['$commonRecords', '$coll2']
            }
        }
    },
    {
        $unwind: '$records'
    },
    {
        $replaceRoot: {
            newRoot: '$records'
        }
    },
    {
        $project: {
            _id: 0,
            name: {
                $ifNull: ['$name', '$coll2.name']
            },
            marks1: {
                $ifNull: ['$marks', 0]
            },
            marks2: {
                $ifNull: ['$coll2.marks', 0]
            }
        }
    },
    {
        $addFields: {
            result: {
                $add: ['$marks1', '$marks2']
            }
        }
    }
])
这是一个示例:

    {
       $lookup:
         {
           from: [collection to join],
           local_Field: [field from the input documents],
           foreign_Field: [field from the documents of the "from" collection],
           as: [output field]
         }
    }
显示链接

您可以使用$unionWith(从4.4开始) 大概是这样的:

db.c1.aggregate([
{$set: {
  mark1: "$marks"
}}, 
{$unionWith: {
  coll: 'c2',
  pipeline: [{$set: {mark2: "$marks"}}]
}}, 
{$group: {
  _id: "$name",
  result: {
    $sum: "$marks"
  },
  mark1: {$first: {$ifNull: ["$mark1", 0]}},
  mark2: {$first: {$ifNull: ["$mark2", 0]}}
}}])

还没有。等待MongoDB 3.6,在那里您可以执行“非相关的”
$lookup
。但与其说“你需要它”,不如说“为什么”。因为即使有了“加入”的功能,您也可以在不需要这些东西的情况下设计应用程序,从而获得更好的应用程序性能。我怎样才能做到这一点呢?我认为添加适当的解释也会对观众有所帮助