简体   繁体   中英

Combine array from embedded document based on condition in MongoDB

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?

Note: I'm making a couple assumptions because your question has some illegal JSON in it. Let me know if I guessed wrong. Also, I'm not on a computer with Mongo right now, so I might have some syntax issues.

db.students.aggregate([
{ $unwind: "$TestDetails" },
{
    $group:{
        _id: { Student: "$Student_id", TestSeq: "$TestDetails.TestSeq},
        Subtest_names: { $addToSet: "$TestDetails.Subtestname" }
    }
},
{
    $project:{
        Student: "$_id.Student",
        TestSeq: "$_id.TestSeq,
        Subtest_names: "$Subtest_names"
    }
}
])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM