Mule 4:Dataweave 2.0:有什么方法可以在 groupBy 方法中使用类似于 SQL GroupBY 和 Having 的过滤条件?

Mule 4 : Dataweave 2.0 : is there any way we can have a filter condition in groupBy method that works similar to SQL GroupBY and Having?

场景:给出如下输入 sampleArray ,我想将所有有特定老师的学生分组。

在 DataWeave 中,我们有一个方法 groupBy 允许我们对指定字符串键的数组进行分组。 但是这里因为 item.studentsMarks.subjectTeacher returns 一个数组,我收到下面指定的错误。

谁能帮忙。提前致谢。

下面附上预期输出。

示例输入:

var sampleArray = [
    {
        "studentName" : "ABC",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "50",
                "subjectTeacher" : "teacher_1"
            },
            {
                "subject" : "science",
                "marks" : "30",
                "subjectTeacher" : "teacher_3"
            }
        ]
    },
    {
        "studentName" : "XYZ",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "90",
                "subjectTeacher" : "teacher_1"

            },
            {
                "subject" : "arts",
                "marks" : "50", 
                "subjectTeacher" : "teacher_2"
            }
        ]
    }
]


尝试过的代码:

payload groupBy ((item, index) -> item.studentsMarks.subjectTeacher)

错误:

Cannot coerce Array to String

预期输出:

[
    
    "teacher_1" : [{
            "studentName" : "ABC",  
            "studentsMarks" : [
                {
                    "subject" : "maths",
                    "marks" : "50",
                    "subjectTeacher" : "teacher_1"
                },
                {
                    "subject" : "science",
                    "marks" : "30",
                    "subjectTeacher" : "teacher_3"
                }
            ]
        },    
        {
                "studentName" : "XYZ",  
                "studentsMarks" : [
                    {
                        "subject" : "maths",
                        "marks" : "90",
                        "subjectTeacher" : "teacher_1"
        
                    },
                    {
                        "subject" : "arts",
                        "marks" : "50", 
                        "subjectTeacher" : "teacher_2"
                    }
                ]
           }
    ],
    "teacher_2" : [
        
                {
                "studentName" : "XYZ",  
                "studentsMarks" : [
                    {
                        "subject" : "maths",
                        "marks" : "90",
                        "subjectTeacher" : "teacher_1"
        
                    },
                    {
                        "subject" : "arts",
                        "marks" : "50", 
                        "subjectTeacher" : "teacher_2"
                    }
                ]
           }
    ],
    "teacher_3" : [
        
{
            "studentName" : "ABC",  
            "studentsMarks" : [
                {
                    "subject" : "maths",
                    "marks" : "50",
                    "subjectTeacher" : "teacher_1"
                },
                {
                    "subject" : "science",
                    "marks" : "30",
                    "subjectTeacher" : "teacher_3"
                }
            ]
        }
    ]
        
]

这里有一个非常快速的方法来完成它。不过,它不仅仅包含 groupBy

%dw 2.0
output application/json

var sampleArray = [
    {
        "studentName" : "ABC",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "50",
                "subjectTeacher" : "teacher_1"
            },
            {
                "subject" : "science",
                "marks" : "30",
                "subjectTeacher" : "teacher_3"
            }
        ]
    },
    {
        "studentName" : "XYZ",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "90",
                "subjectTeacher" : "teacher_1"

            },
            {
                "subject" : "arts",
                "marks" : "50", 
                "subjectTeacher" : "teacher_2"
            }
        ]
    }
]
---
// Get list of all teachers
sampleArray..*subjectTeacher
// Change the collection to a set 
distinctBy $ 
// Iterate over every single teacher and create an object where
// the field is the teacher while the value is a collection
// containing all students where they attend a class for the teacher.
map {
    ($): (sampleArray dw::core::Arrays::partition (e) -> e..*subjectTeacher contains $).success
}