将文档的嵌套数组更新为对象

时间:2020-06-22 14:07:55

标签: mongodb mongodb-query aggregation-framework

我有一个名为“ A”的集合,其中包含成千上万的文档。成绩是由子文档组成的数组,这些子文档包含属性问题,这些属性问题是如下所示的数字数组

public class PnFMathQuiz extends JFrame{
void playSound(String musicLocation)
{
    try {
        File musicPath = new File (musicLocation);

        if (musicPath.exists()){
            AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
            Clip clip = AudioSystem.getClip();
            clip.open(audioInput);
            clip.start();
            clip.loop(Clip.LOOP_CONTINUOUSLY);
        }
        else {
            System.out.println("Can't find file");
        }
    }
    catch (Exception ex){
        ex.printStackTrace();
    }
}
JFrame frame = new JFrame();

PnFMathQuiz() {
    prepareGUI();
}

public void prepareGUI() {
    frame.setTitle("The Phineas and Ferb MATH QUIZ");
    frame.setSize(1500, 1000);
    frame.getContentPane().setLayout(null);
    frame.setContentPane(new JLabel(new ImageIcon("BG2.JPG")));
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);   

public static void main(String[] args) {
    String filepath = "Quirky Worky Song.wav";

    PnFMathQuiz musicObject = new PnFMathQuiz();
    musicObject.playSound(filepath);
}  }

为了添加其他功能,我们必须修改问题的格式,并将其设置为包含两个字段的文档数组,其中两个字段是 number (它将继承旧数据),而另一个字段是< strong>尝试(默认情况下,它将为空数组,并且可能具有值“ P”(通过)或“ F”(失败)。转换后,文档应如下图所示

{ "_id" : 1,
      "grades" : [
        { type: "quiz", questions: [ 10, 8, 5 ] },
        { type: "quiz", questions: [ 8, 9, 6 ] },
        { type: "hw", questions: [ 5, 4, 3 ] },
        { type: "exam", questions: [ 25, 10, 23, 0 ] }
      ]
   }

感谢所有帮助

1 个答案:

答案 0 :(得分:1)

您可以在这里进行自我测试:https://mongoplayground.net/p/ZEp_vk5hhf7

简要说明:
您必须使用$map两次。

  • 首先,您必须循环$grades字段,因为他是一个数组。
  • 在遍历$grades数组时,您必须遍历$questions字段,因为他也是数组。

查询:

db.collection.aggregate([
  {
    "$project": {
      "grades": {
        "$map": {
          "input": "$grades",
          "as": "g",
          "in": {
            "type": "$$g.type",
            "questions": {
              "$map": {
                "input": "$$g.questions",
                "as": "q",
                "in": {
                  "number": "$$q",
                  "attempts": []
                }
              }
            }
          }
        }
      }
    }
  }
])

结果:

[
  {
    "_id": 1,
    "grades": [
      {
        "questions": [
          {
            "attempts": [],
            "number": 10
          },
          {
            "attempts": [],
            "number": 8
          },
          {
            "attempts": [],
            "number": 5
          }
        ],
        "type": "quiz"
      },
      {
        "questions": [
          {
            "attempts": [],
            "number": 8
          },
          {
            "attempts": [],
            "number": 9
          },
          {
            "attempts": [],
            "number": 6
          }
        ],
        "type": "quiz"
      },
      {
        "questions": [
          {
            "attempts": [],
            "number": 5
          },
          {
            "attempts": [],
            "number": 4
          },
          {
            "attempts": [],
            "number": 3
          }
        ],
        "type": "hw"
      },
      {
        "questions": [
          {
            "attempts": [],
            "number": 25
          },
          {
            "attempts": [],
            "number": 10
          },
          {
            "attempts": [],
            "number": 23
          },
          {
            "attempts": [],
            "number": 0
          }
        ],
        "type": "exam"
      }
    ]
  }
]