【问题标题】:Full Outer join in MongoDBMongoDB 中的完全外连接
【发布时间】:2021-03-26 23:03:00
【问题描述】:

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

[更新:]

我想从 Collection1 和 Collection2 中获得如下附件的结果:

示例:Result Required

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

【问题讨论】:

  • 还没有。等待 MongoDB 3.6,您可以在其中执行“非相关”$lookup。但与其说“你需要它”,不如说“为什么”是正当的。因为即使有“加入”的便利,通过以不需要这样的方式进行设计,您将获得更好的应用程序性能。
  • 您好 Neil,我已附上样本集和所需结果。我怎样才能做到这一点?

标签: mongodb join outer-join nosql


【解决方案1】:

您可以使用 $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]}}
}}])

【讨论】:

    【解决方案2】:

    我已将集合命名为 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']
                }
            }
        }
    ])
    

    【讨论】:

    • 我认为添加适当的解释也会对观众有所帮助
    【解决方案3】:

    这是一个示例:

        {
           $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]
             }
        }
    

    显示this链接

    【讨论】:

      猜你喜欢
      • 2010-09-20
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 2013-03-11
      • 2022-01-01
      • 2022-01-16
      相关资源
      最近更新 更多