如何在mongodb聚合中基于另一个数组对数组进行分组? [英] How to group arrays based on another array in mongodb aggregation?

查看:40
本文介绍了如何在mongodb聚合中基于另一个数组对数组进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过类似的问题,但是它没有答案,因此无法回答我的问题.

I have seen kind of a similar question, but it has no answer so it doesn't answer my question.

如何在mongodb聚合中基于另一个数组对数组进行分组?

How to group arrays based on another array in mongodb aggregation?

假设您具有以下架构:

PollSchema

PollSchema

const PollSchema = new mongoose.Schema({
    numberOfChoices: {
        type: Number,
        required: true,
        min: [1, "Number of choices must be at least 1"],
        max: [10, "Number of choices cannot be more than 10"],
    },
    arrayOfNamesOfChoices:[{
        name: {
            type: String,
            required: true,
        },
    }],
});

CastedVotedSchema

CastedVotedSchema

const CastedVoteSchema = new mongoose.Schema({
    voter: {
        type: String,
        required: true,
    },
    choice: {
        type: Number,
        required: true,
        min:0,
        max:9
    },
});

在mongodb聚合中,我如何根据选择对数组中的castedVote进行排序.例如,考虑以下数据示例:

How can I, in mongodb aggregation, sort the castedVotes in arrays based on their choice. For example consider the data sample below:

Poll = {
    numberOfChoices: 3
    arrayOfNamesOfChoices: [
        {name:'choiceA'},{name:'choiceB'},{name:'choiceC'}
    ]
}

CastedVotes = [
    {voter: 'Juzi', choice: 0},{voter: 'Juma', choice: 0},{voter: 'Jane', choice: 0},
    {voter: 'Jamo', choice: 1},{voter: 'Juju', choice: 1},{voter: 'Jana', choice: 1},
]

注意:

NOTE: There are no votes casted in choiceC

我正在尝试创建一个聚合查询,该查询将基于每个选择返回一个数组.例如(见下文).

I am trying to create an aggregation query that returns an array based on each choice. For example (see below).

//below is the result I desire to have 
{ 
    totalVotes:[
        {voter: 'Juzi', choice: 0},{voter: 'Juma', choice: 0},{voter: 'Jane', choice: 0},
        {voter: 'Jamo', choice: 1},{voter: 'Juju', choice: 1},{voter: 'Jana', choice: 1},
    ], 
    choiceA:[{voter: 'Juzi', choice: 0},{voter: 'Juma', choice: 0},{voter: 'Jane', choice: 0}],
    choiceB:[{voter: 'Jamo', choice: 1},{voter: 'Juju', choice: 1},{voter: 'Jana', choice: 1}],
    choiceC:[],// the choiceC array should be present and empty
}

如何在mongodb聚合中将基于 CastedVote 数组的 Poll.arrayOfNamesOfChoices 数组分组?

How to group the CastedVote array based Poll.arrayOfNamesOfChoices array in mongodb aggregation?

推荐答案

如何在mongodb聚合中将基于CastedVote数组的Poll.arrayOfNamesOfChoices数组分组?

How to group the CastedVote array based Poll.arrayOfNamesOfChoices array in mongodb aggregation?

如果您有一个名为 poll 的集合,其中包含如下文档

If you have a collection called poll, containing a document as below

{ "_id": 100,
  "choices": [
    { "name": "choiceA", "id": 0 },
    { "name": "choiceB", "id": 1 },
    { "name": "choiceC", "id": 2 } ]
}

另一个名为 castedvote 的集合,其中包含以下文档:

And another collection called castedvote, containing documents as below:

{"voter": "Juzi", "choice": 0, "pollId": ObjectId("...")}
{"voter": "Juma", "choice": 0, "pollId": ObjectId("...")}
{"voter": "Jane", "choice": 0, "pollId": ObjectId("...")}
{"voter": "Jamo", "choice": 1, "pollId": ObjectId("...")}
{"voter": "Juju", "choice": 1, "pollId": ObjectId("...")}
{"voter": "Jana", "choice": 1, "pollId": ObjectId("...")}

您可以在下面执行聚合:

You can perform aggregation below:

db.poll.aggregate([
    {"$match": {"_id": 100}}, 
    {"$unwind":"$choices"},
    {"$lookup": {
        "from":"castedvotes", 
        "let": {"c":"$choices"}, 
        "pipeline":[
            {"$match":
                {"$expr":
                    {"$eq": ["$choice", "$$c.id"]},
            }},
        ], 
        "as":"voters"
    }},
    {"$addFields":{
        "x": {
            "$arrayToObject": [ [{"k": "$choices.name", "v": "$voters"}] ]
        }
    }}, 
    {"$replaceRoot": {"newRoot": "$x"}}, 
]);    

使用 $ arrayToObject 从中创建动态字段名称字段值.输出为:

Using $arrayToObject to create a dynamic field name from the field values. The output would be:

{  "choiceA": [
        {"_id": ObjectId("..."), "voter": "Juzi", "choice": 0, "pollId": 100 },
        {"_id": ObjectId("..."), "voter": "Juma", "choice": 0, "pollId": 100 },
        {"_id": ObjectId("..."), "voter": "Jane", "choice": 0, "pollId": 100 },
]},
{  "choiceB": [
        {"_id": ObjectId("..."), "voter": "Jamo", "choice": 1, "pollId": 100 },
        {"_id": ObjectId("..."), "voter": "Juju", "choice": 1, "pollId": 100 },
        {"_id": ObjectId("..."), "voter": "Jana", "choice": 1, "pollId": 100 }
]},
{  "choiceC": [ ] }

这篇关于如何在mongodb聚合中基于另一个数组对数组进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆