I have a collection of student details like below:
{
"Student_id": 1,
"StudentName": "ABC",
"TestDetails": [{
"SubtestName":"Reading", "TestSeq":1, "SubTestDetails":1,
"Scores":[{"ScoreType":"YY","ScoreValue":"100"},{"ScoreType":"XX","ScoreValue":"100"},
{"ScoreType": "ZZ","ScoreValue":"100"}]}]
,
"TestDetails": [{
"SubtestName":"Writing", "TestSeq":1, "SubTestDetails":2,
"Scores":[{"ScoreType":"YY","ScoreValue":"200"},{"ScoreType":"XX","ScoreValue":"200"},
{"ScoreType": "ZZ","ScoreValue":"200"}]}]
,
"TestDetails": [{
"SubtestName":"Listning", "TestSeq":2, "SubTestDetails":3,
"Scores":[{"ScoreType":"YY","ScoreValue":"300"},{"ScoreType":"XX","ScoreValue":"300"},
{"ScoreType": "ZZ","ScoreValue":"300"}]}]
,
"TestDetails": [{
"SubtestName":"Speaking", "TestSeq":2, "SubTestDetails":4,
"Scores":[{"ScoreType":"YY","ScoreValue":"400"},{"ScoreType":"XX","ScoreValue":"400"},
{"ScoreType": "ZZ","ScoreValue":"400"}]}]
,
"TestDetails": [{
"SubtestName":"Smartness", "TestSeq":3, "SubTestDetails":5,
"Scores":[{"ScoreType":"YY","ScoreValue":"500"},{"ScoreType":"XX","ScoreValue":"500"},
{"ScoreType": "ZZ","ScoreValue":"500"}]}]
},
{
"Student_id": 2,
"StudentName": "XYZ",
"TestDetails": [{
"SubtestName":"Smartness", "TestSeq":1, "SubTestDetails":1,
"Scores":[{"ScoreType":"YY","ScoreValue":"100"},{"ScoreType":"XX","ScoreValue":"100"},
{"ScoreType": "ZZ","ScoreValue":"100"}]}]
,
"TestDetails": [{
"SubtestName":"Writing", "TestSeq":1, "SubTestDetails":2,
"Scores":[{"ScoreType":"YY","ScoreValue":"200"},{"ScoreType":"XX","ScoreValue":"200"},
{"ScoreType": "ZZ","ScoreValue":"200"}]}]
,
"TestDetails": [{
"SubtestName":"Listning", "TestSeq":2, "SubTestDetails":3,
"Scores":[{"ScoreType":"YY","ScoreValue":"300"},{"ScoreType":"XX","ScoreValue":"300"},
{"ScoreType": "ZZ","ScoreValue":"300"}]}]
,
"TestDetails": [{
"SubtestName":"Speaking", "TestSeq":2, "SubTestDetails":4,
"Scores":[{"ScoreType":"YY","ScoreValue":"400"},{"ScoreType":"XX","ScoreValue":"400"},
{"ScoreType": "ZZ","ScoreValue":"400"}]}]
,
"TestDetails": [{
"SubtestName":"Reading", "TestSeq":3, "SubTestDetails":5,
"Scores":[{"ScoreType":"YY","ScoreValue":"100"},{"ScoreType":"XX","ScoreValue":"100"},
{"ScoreType": "ZZ","ScoreValue":"1000"}]}]
},
.
.
.
)
How can I create aggregate query to generate document like below:
{Student:1, "TestSeq" : 1, [{Subtest_name: Reading},{Subtest_name: Writing}]},
{Student:1,"TestSeq" : 2, [{Subtest_name: Listning},{Subtest_name: Speaking}]},
{Student:1, "TestSeq" : 3, [{Subtest_name: Smartness}]},
{Student:2, "TestSeq" : 1, [{Subtest_name: Smartness},{Subtest_name: Writing}]},
{Student:2, "TestSeq" : 2, [{Subtest_name: Listning},{Subtest_name: Speaking}]},
{Student:2, "TestSeq" : 3, [{Subtest_name: Reading}]},
{Student:3, "TestSeq" : 1, [{Subtest_name: Subtest1},{Subtest_name: Subtest2}]},
{Student:3, "TestSeq" : 2, [{Subtest_name: Subtest3},{Subtest_name: Subtest4}]},
{Student:3, "TestSeq" : 3, [{Subtest_name: Subtest5}]}
Logic is to combine/group Subtest name based on TestSeq values. For example Subtest names are combined for TestSeq = 1, for value 2 it's in 2nd row and 3 for last Subtest name for each student.
How can I implement that?
I have tried as below -
db.students.aggregate([
{$unwind: "$SubtestAttribs"},
{ $project: { student_name: 1, student_id : 1,
print_ready : "$SubtestAttribs.TestSeq",
Subtest_names :$SubtestAttribs.SubtestName" } } ])
But I am unable to form array based on condition. Above snippet giving data for each test seq. But how to combine two sub test name based on test seq?
TestDetailsmultiple times in a single document. Thats not really legal JSON since the keys are not unique... are you sure thats what your schema really looks like? I think its suppose to be a single keyTestDetailsthat is an array of all theTestDetailsyou currently have in one.[{Subtest_name: Subtest5}]) is missing a key.