私のデータベース スキーマは次のようになります。
{
"_id" : 137,
"name" : "Tamika Schildgen",
"scores" : [
{
"score" : 4.433956226109692,
"type" : "exam"
},
{
"type" : "quiz",
"score" : 65.50313785402548
},
{
"type" : "homework",
"score" : 89.5950384993947
},
{
"type" : "homework",
"score" : 54.75994689226145
}
]
}
Bson filter = (Filters.eq("scores.type", "homework"));
Bson projection = Projections.slice("scores", 2, 2);
Bson sort = Sorts.ascending("_id");
List<Document> all = collection.find(filter).projection(projection).sort(sort).into(new ArrayList<Document>());
for (Document cur : all) {
List<Document> score = (List<Document>) cur.get("scores");
List marks = new ArrayList<Integer>();
for (Document hw1 : score) {
marks.add(hw1.get("score"));
}
Collections.sort(marks);
double x = (Double) marks.get(0);
Bson find = Filters.eq("_id", cur.get("_id"));
Bson update = new Document("$pull", new Document("scores", new Document("scores.type", "homework").append("scores.score" , x)));
collection.updateOne(find,update);
System.out.println("deleted " + cur.get("_id") + " " + x);
質問では、各生徒の宿題の点数が最も低いものを削除するように求められます。私のアプローチは次のとおりです。
- 最初に、生徒の ID と宿題の点数のみを含むドキュメントのリストを取得します。
- 各学生の最小 hw スコアを取得します。
- その最低宿題スコアを含むサブドキュメントを引き出します。
印刷物には各生徒の宿題の最小スコアが正しく表示されますが、プル クエリがデータベースから値を削除していません。ここで何が欠けていますか?解決策を求めるのではなく、ここで私が間違っていることについてのヒントが役立ちます。ありがとう。