简体   繁体   English

根据MongoDB中的条件从嵌入式文档合并数组

[英]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. 逻辑是根据TestSeq值对子测试名称进行组合/分组。 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. 例如,为TestSeq = 1组合了子测试名称,对于值2,它位于第二行,对于每个学生,最后一个子测试名称是3。

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. 注意:我做几个假设,因为您的问题中包含一些非法的JSON。 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. 另外,我现在不在装有Mongo的计算机上,因此我可能会遇到一些语法问题。

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"
    }
}
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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