带有条件group by语句的MongoDB查询

2024-02-17

我需要从 mongoDB 数据库导出客户记录。导出的客户记录不应具有重复的值。 “firstName+lastName+code”是对记录进行重复数据删除的关键,如果数据库中存在两条具有相同键的记录,那么我需要优先考虑具有电子邮件以外的值的源字段。

顾客 (id,firstName,lastName,code,source) 收藏是这个。

如果有 3 条记录具有相同的唯一键和 3 个不同的来源,那么我只需要在 2 个来源(电视、互联网)之间选择一条记录{或者如果有 n 个来源,我只需要一条记录}而不是 ' email'(当只有一条记录具有唯一密钥并且来源是电子邮件时,将选择电子邮件) 查询使用:

db.customer.aggregate([
    {
        "$match": {
            "active": true,
            "dealerCode": { "$in": ["111391"] },
            "source": { "$in": ["email", "TV", "internet"] }
        }
    },
    {
        $group: {
            "_id": {
                "firstName": "$personalInfo.firstName",
                "lastName": "$personalInfo.lastName",
                "code": "$vehicle.code"
            },
            "source": {
                $addToSet: { "source": "$source" }
            }
        }
    },
    {
        $redact:
        {
            $cond: [
                { $eq: [{ $ifNull: ["$source", "other"] }, "email"] },
                "$$PRUNE",
                "$$DESCEND"
            ]
        }
    },
    {
        $project:
        {
            "source":
            {
                $map:
                {
                    "input": {
                        $cond: [
                            { $eq: [{ $size: "$source" }, 0] },
                            [{ "source": "email" }],
                            "$source"
                        ]
                    },
                    "as": "inp",
                    "in": "$$inp.source"
                }
            },
            "record": { "_id": 1 }
        }
    }
])

示例输出:

{ "_id" : { "firstName" : "sGI6YaJ36WRfI4xuJQzI7A==", "lastName" : "99eQ7i+uTOqO8X+IPW+NOA==", "code" : "1GTHK23688F113955" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "WYDROTF/9vs9O7XhdIKd5Q==", "lastName" : "BM18Uq/ltcbdx0UJOXh7Sw==", "code" : "1G4GE5GV5AF180133" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "id+U2gYNHQaNQRWXpe34MA==", "lastName" : "AIs1G33QnH9RB0nupJEvjw==", "code" : "1G4GE5EV0AF177966" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "qhreJVuUA5l8lnBPVhMAdw==", "lastName" : "petb0Qx3YPfebSioY0wL9w==", "code" : "1G1AL55F277253143" }, "source" : ["TV"] }
{ "_id" : { "firstName" : "qhreJVuUA5l8lnBPVhMAdw==", "lastName" : "6LB/NmhbfqTagbOnHFGoog==", "code" : "1GCVKREC0EZ168134" }, "source" : ["TV", "internet"] }

这是此查询的问题,请提出建议:(


你的代码不起作用,因为$cond http://docs.mongodb.org/manual/reference/operator/aggregation/cond/不是累加器运算符。仅有的these http://docs.mongodb.org/manual/reference/operator/aggregation/group/#accumulator-operator累加器运算符,可用于$group stage.

假设您的记录包含不超过两个可能的值source正如您在问题中提到的,您可以添加条件$project阶段并修改$group阶段为,

Code:

    db.customer.aggregate([
        {
            $group: {
                "_id": {
                    "id": "$id",
                    "firstName": "$firstName",
                    "lastName": "$lastName",
                    "code": "$code"
                },
                "sourceA": { $first: "$source" },
                "sourceB": { $last: "$source" }
            }
        },
        {
            $project: {
                "source": {
                    $cond: [
                        { $eq: ["$sourceA", "email"] },
                        "$sourceB",
                        "$sourceA"
                    ]
                }
            }
        }
    ])

如果源的可能值超过两个,那么您可以执行以下操作:

  • Group by the id, firstName, lastName and code。积累 的独特价值source, 使用$addToSet http://docs.mongodb.org/manual/reference/operator/aggregation/addToSet/操作员。
  • Use $redact http://docs.mongodb.org/manual/reference/operator/aggregation/redact/仅保留除email.
  • Project必填字段,如果source数组为空(所有元素已被删除),添加一个 价值email to it.
  • Unwind源字段将其列为字段而不是数组。 (选修的)

Code:

    db.customer.aggregate([
        {
            $group: {
                "_id": {
                    "id": "$id",
                    "firstName": "$firstName",
                    "lastName": "$lastName",
                    "code": "$code"
                },
                "sourceArr": { $addToSet: { "source": "$source" } }
            }
        },
        {
            $redact: {
                $cond: [
                    { $eq: [{ $ifNull: ["$source", "other"] }, "email"] },
                    "$$PRUNE",
                    "$$DESCEND"
                ]
            }
        },
        {
            $project: {
                "source": {
                    $map: {
                        "input":
                        {
                            $cond: [
                                { $eq: [{ $size: "$sourceArr" }, 0] },
                                [{ "source": "item" }],
                                "$sourceArr"]
                        },
                        "as": "inp",
                        "in": "$$inp.source"
                    }
                }
            }
        }
    ])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有条件group by语句的MongoDB查询 的相关文章

随机推荐