mongo query:当对象具有匹配值时删除对象(电子邮件)

时间:2017-10-02 14:55:33

标签: mongodb aggregation-framework

我正在尝试找到类型== 1(已经工作)的所有对象,然后检查电子邮件地址中的重复项。 (不工作)

输出将是这样的:

[ { _id:
 { id: 59d0e3f728150e39b008013f,
   firstName: 'pera',
   lastName: 'pekko',
   phone: '04040404040',
   massageType: 'jipii massage',
   time: 45,
   price: 30,
   startsAt: '2017-10-02T11:00:00+03:00',
   endsAt: '2017-10-02T11:45:00+03:00' },
email: [ 'gg@gmail.com' ],
{ _id:
 { id: 59ce25975f4d1a096cdd2733,
   firstName: 'sdad',
   lastName: 'autismus',
   phone: '04040404040',
   massageType: 'jipii massage',
   time: 60,
   price: 33,
   startsAt: '2017-10-02T12:45:00+03:00',
   endsAt: '2017-10-02T13:45:00+03:00' },
email: [ 'YES@hotmail.com' ]
}]

所以基本上它会检查重复的“电子邮件”值。如果存在重复值 - >从结果中删除WHOLE对象。

到目前为止我的代码(结果打印所有类型== 1个具有重复项的对象):

        db3.massagerTimes.aggregate(
        {$match: {_id: mongo.helper.toObjectID(data.timesData)}},
        {$unwind: "$times"},
        {$match: {"times.type": 1}},
        {
            $group: {
                _id: {
                    id: "$times._id",
                    firstName: "$times.firstName",
                    lastName: "$times.lastName",
                    phone: "$times.phone",
                    massageType: "$times.massageType",
                    time: "$times.time",
                    price: "$times.price",
                    startsAt: "$times.startsAt",
                    endsAt: "$times.endsAt"
                },
                email: {$addToSet: "$times.email"},
                count: {"$sum": 1}
            }
        },
        {
            $match: {
                count: {"$lte": 1}
            }
        },
        {$sort: {"times._id": -1}},
        function (err, result) {
            if (err)
                throw err;

            if (result) {
                console.log(result);
                res.json(result);
            }
        });
}

收集:

{
"_id": {
    "$oid": "59ca4eb610b3be1f8499f6d5"
},
"times": [
    {
        "_id": {
            "$oid": "59ce274f5f4d1a096cdd2745"
        },
        "type": 0,
        "startsAt": "2017-10-02 13:30 +03:00",
        "endsAt": "2017-10-02 13:45 +03:00"
    },
    {
        "_id": {
            "$oid": "59ce274f5f4d1a096cdd2744"
        },
        "type": 0,
        "startsAt": "2017-10-02 13:15 +03:00",
        "endsAt": "2017-10-02 13:30 +03:00"
    },
    {
        "_id": {
            "$oid": "59ce28415f4d1a096cdd275b"
        },
        "title": "sdad autismus",
        "firstName": "sdad",
        "lastName": "autismus",
        "type": 1,
        "email": "YES@hotmail.com",
        "phone": "04040404040",
        "massageType": "jipii massage",
        "time": 45,
        "price": 30,
        "additionalInfo": null,
        "startsAt": "2017-10-01T08:30:00+03:00",
        "endsAt": "2017-10-01T09:15:00+03:00"
    },
    {
        "_id": {
            "$oid": "59ce28665f4d1a096cdd275e"
        },
        "title": "sdad autismus",
        "firstName": "sdad",
        "lastName": "autismus",
        "type": 1,
        "email": "YES@hotmail.com",
        "phone": "04040404040",
        "massageType": "jipii massage",
        "time": 45,
        "price": 30,
        "additionalInfo": null,
        "startsAt": "2017-10-01T09:30:00+03:00",
        "endsAt": "2017-10-01T10:15:00+03:00"
    },
    {
        "_id": {
            "$oid": "59d0e3f728150e39b008013f"
        },
        "title": "pera pekko",
        "firstName": "pera",
        "lastName": "pekko",
        "type": 1,
        "email": "gg@gmail.com",
        "phone": "04040404040",
        "massageType": "some massage",
        "time": 45,
        "price": 30,
        "additionalInfo": null,
        "startsAt": "2017-10-02T11:00:00+03:00",
        "endsAt": "2017-10-02T11:45:00+03:00"
    }
]
}

问题:如何从值中检查重复项,然后从结果中删除它? (在这种情况下,YES @ hotmail.com< - 2x,删除重复的对象,而不仅仅是重复的值)

1 个答案:

答案 0 :(得分:1)

您需要的核心就是:

posview