MongoDB聚合和查找问题

时间:2018-07-03 21:33:23

标签: mongodb aggregation-framework

我有以下收藏:

"campaign": [{
    "_id": "5b06c3f99b7997436714b230",
    "recipients": [
        {
            "individual": "5a7075e8b36da12a64dc20ca",
            "_id": "5b06cce20f93e2216cf5c189",
            "sent": false
        },
        {
            "individual": "5a7075e8b36da12a64dc20d6",
            "_id": "5b06cce20f93e2216cf5c18a",
            "sent": true
        }
    ]
}]

我在使用标准查找查询来查找send = false / true的项目时遇到了麻烦,因此选择了汇总。

model.aggregate(
            {
                $addFields: {
                    "recipients": {
                        $filter: { // We override the existing field!
                            input: "$recipients",
                            as: "recipients",
                            cond: { $eq: ["$$recipients.sent", true] }
                        }
                    }
                }
            },
            {
                $lookup: {
                    from: 'registrations',
                    localField: 'recipients.individual',
                    foreignField: '_id',
                    as: 'recipients.individual'
                }
            }
        )

不幸的是,这导致:

"campaign": [{
        "_id": "5b06c3f99b7997436714b230",
        "recipients": {
            "individual": [
                {
                    "_id": "5a7075e8b36da12a64dc20ca",
                    ...
                    ...
                    "registrationDate": "2012-01-08T00:00:00.000Z"
                },
                {
                    "_id": "5a7075e8b36da12a64dc20d6",
                    ...
                    ...
                    "registrationDate": "2012-01-06T00:00:00.000Z"
                }
            ]
        }
}]

代替必需的:

"campaign": [
{
    "_id": "5b06c3f99b7997436714b230",
    "recipients": [
        {
            "individual": {
                    "_id": "5a7075e8b36da12a64dc20ca",
                    ...
                    ...
                    "registrationDate": "2012-01-08T00:00:00.000Z"
            },
            "_id": "5b06cce20f93e2216cf5c189",
            "sent": false
        },
        {
            "individual": {
                    "_id": "5a7075e8b36da12a64dc20d6",
                    ...
                    ...
                    "registrationDate": "2012-01-06T00:00:00.000Z"
            },
            "_id": "5b06cce20f93e2216cf5c18a",
            "sent": true
        }
    ]
}]

关于另一个注意事项,就像我提到的那样,我尝试运行常规查找,以下是一些方法,这些方法基本上返回了所有数据,并且未按预期进行过滤。如果您能解释为什么我会很感激。

 db.campaigns.find( { "recipients": { $elemMatch: { sent: false} } } )
 db.campaigns.find( { "recipients.sent": {$ne: true} } )
 db.campaigns.find({$or:[{"recipients.sent":false},{"recipients.sent":{$exists:false}}]})
 db.campaigns.find({"recipients.sent": {$in: [null, false]}})

嘿@Veeram,感谢您在这里为我提供的帮助...为了使其正常工作,我必须进行一些调整,因为您手边没有所有的代码/模型,对您来说也没有错。

这是我想出的:

model.aggregate(
            // {$match: {"campaign.id": campaignId}},
            {
                $unwind: {
                    path: '$recipients',
                    includeArrayIndex: 'arrayIndex',
                    preserveNullAndEmptyArrays: false
                }
            }, {
                $match: {
                    'recipients.sent': {
                        $eq: true
                    }
                }
            }, {
                $sort: {
                    total: -1
                }
            }, {
                $limit: 5
            }, {
                $lookup: {
                    from: 'registrations',
                    localField: 'recipients.individual',
                    foreignField: '_id',
                    as: 'recipients.individual'
                }
            }, {
                $group: {
                    _id: '$_id',
                    recipients: {
                        $push: '$recipients'
                    },
                    total: {
                        $sum: 1
                    }
                }
            }
        )

这似乎可以解决问题。

现在唯一的另一件事是,我只想包含“个人”的某些属性,例如姓名和电子邮件地址。

非常感谢您的帮助。

0 个答案:

没有答案
相关问题