MongoDB查询集合组的集合差异

时间:2019-01-09 17:57:06

标签: mongodb mongodb-query difference

给出一组modified条记录和一组original条记录。我希望能够编写一个查询,从本质上给我从original“集合”到modified“集合”的集合差异。

因此,给定了originalmodified这两个集合:

{ "_id" : 1, "set": "original", "key" : "foo", "element" : "bar" }
{ "_id" : 2, "set": "original", "key" : "bar", "element" : "old" }
{ "_id" : 3, "set": "original", "key" : "qux", "element" : "abc" } # Deleted

{ "_id" : 4, "set": "modified", "key" : "foo", "element" : "bar" } # Unchanged
{ "_id" : 5, "set": "modified", "key" : "bar", "element" : "new" } # Changed
{ "_id" : 6, "set": "modified", "key" : "baz", "element" : "bar" } # Created

我想从差异查询中获得某种结果,它们看起来像是一组可移动的文档,例如:

{ "_id" : 3, "deleted": True}
{ "_id" : 5, "changed": True}
{ "_id" : 6, "created": True}

或更不理想(只是因为它不具备cursor功能):

{
    "deleted": [3],
    "changed": [5],
    "created": [6]
}

我不限于结果格式,而只是想弄清楚如何进行此计算,并想证明我在寻找什么。

我看到Mongo具有$setDifference功能,但是在解决问题时遇到了困难。

1 个答案:

答案 0 :(得分:1)

您可以使用以下汇总:

db.col.aggregate([
    {
        $group: {
            _id: "$key",
            docs: { $push: "$$ROOT" },
            lastId: { $last: "$_id" }
        }
    },
    {
        $project: {
            _id: 1,
            lastId: 1,
            original: { $arrayElemAt: [ { $filter: { input: "$docs", as: "d", cond: { $eq: [ "$$d.set", "original" ] } } } , 0 ] },
            modified: { $arrayElemAt: [ { $filter: { input: "$docs", as: "d", cond: { $eq: [ "$$d.set", "modified" ] } } } , 0 ] }
        }
    },
    {
        $project: {
            _id: 1,
            lastId: 1,
            state: {
                $switch: {
                    branches: [
                        { case: { $eq: [ "$original", undefined ] }, then: "created" },
                        { case: { $eq: [ "$modified", undefined ] }, then: "deleted" },
                        { case: { $ne: [ "$modified.element", "$original.element" ] }, then: "changed" }
                    ],
                    default: "notModified"
                }
            }
        }
    },
    {
        $group: {
            _id: "$state",
            ids: { $push: "$lastId" }
        }
    },
    {
        $match: {
            _id: { $ne: "notModified" }
        }
    },
    {
        $group: {
            _id: null,
            stats: { $push: { k: "$_id", v: "$ids" } }
        }
    },
    {
        $replaceRoot: {
            newRoot: {
                $arrayToObject: "$stats"
            }
        }
    }
])

最初,您需要将$group$filter结合使用,以获取每个modified的{​​{1}}和original字段。然后,您可以使用$switch根据这两个字段确定状态。最终,您可以再次key(通过此$group),并将$arrayToObject$replaceRoot运算符一起使用,以根据检测到的状态动态获取密钥。最终结果:

state

编辑:或者,您可以使用以下聚合方式每个键获取单个文档:

{ "deleted" : [ 3 ], "changed" : [ 5 ], "created" : [ 6 ] }

输出:

db.col.aggregate([
    {
        $group: {
            _id: "$key",
            docs: { $push: "$$ROOT" },
            lastId: { $last: "$_id" }
        }
    },
    {
        $project: {
            _id: 1,
            lastId: 1,
            original: { $arrayElemAt: [ { $filter: { input: "$docs", as: "d", cond: { $eq: [ "$$d.set", "original" ] } } } , 0 ] },
            modified: { $arrayElemAt: [ { $filter: { input: "$docs", as: "d", cond: { $eq: [ "$$d.set", "modified" ] } } } , 0 ] }
        }
    },
    {
        $project: {
            _id: 1,
            lastId: 1,
            state: {
                $switch: {
                    branches: [
                        { case: { $eq: [ "$original", undefined ] }, then: "created" },
                        { case: { $eq: [ "$modified", undefined ] }, then: "deleted" },
                        { case: { $ne: [ "$modified.element", "$original.element" ] }, then: "changed" }
                    ],
                    default: "notModified"
                }
            }
        }
    },
    {
        $match: {
            state: { $ne: "notModified" }
        }
    },
    {
        $project: {
            _id: "$lastId",
            state: 1
        }
    }
])