This is how my sample QUESTION array is:
[
{
"subject": "app development",
"topic": "dart",
"subtopic": "flutter2",
"questionType": "EASY",
"questionTitle": "how angular works",
},
{
"subject": "app development",
"topic": "dart",
"subtopic": "flutter",
"questionType": "EASY",
"questionTitle": "how angular works",
},
{
"subject": "app development",
"topic": "javascript",
"subtopic": "react native",
"questionType": "EASY",
"questionTitle": "how angular works",
},
]
I need to group the subject field into, and topic into an array.
This is how I want:
{
"subject": "web development",
"topics":[{
topicName:"javascript",
subtopics:[
"react native,
"flutter"
]
}]
}
I'm trying to do it this way but this error:
"Unrecognized expression '$push'" shows
{
$group: {
_id: "$subject",
topics: {
$addToSet: {
title: "$topic",
sub: {$push:"$subtopic"},
},
},
},
},
I need to get an array of grouped subjects and topics and subtopics.