gpt4 book ai didi

java - MongoDB:UpdateOne 记录

转载 作者:行者123 更新时间:2023-11-30 07:38:52 28 4
gpt4 key购买 nike

我编写了以下程序,通过删除最低成绩来更新一组记录。根据程序的输出,UpdateResult.matchedCount=1,但UpdateResult.ModifiedCount=0。本质上,查询是查找要更新的记录,但不是更新记录。

public static void main(String[] args) {

MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("school");
MongoCollection<Document> collection = database.getCollection("students");

FindIterable<Document> iterable = collection.find(new Document("scores.type", "homework")).sort(Sorts.orderBy(Sorts.ascending("_id")));

MongoCursor<Document> iterableDocument = iterable.iterator();

while (iterableDocument.hasNext()) {
Document wholeDocument = (Document) iterableDocument.next();
List<Document> scores = (List<Document>) wholeDocument.get("scores");

System.out.println("Student id: " + wholeDocument.get("_id"));
System.out.println("Student name: " + wholeDocument.get("name"));

for (final Document score1 : scores) {
for (final Document score2 : scores) {
if (score1.get("type").toString().equalsIgnoreCase("homework") && score2.get("type").toString().equalsIgnoreCase("homework")) {

Double homeworkScore1 = (Double) score1.get("score");
Double homeworkScore2 = (Double) score2.get("score");

if (homeworkScore1 < homeworkScore2) {
BasicDBList homeworkScores = new BasicDBList();
homeworkScores.add(new BasicDBObject("type", "homework").append("score", homeworkScore1));
BasicDBObject lowScoreToRemove = new BasicDBObject("scores", homeworkScores);

System.out.println("Lowest Homework score is: " + homeworkScore1);
System.out.println("Document that will be updated: " + wholeDocument);
System.out.println("Pull Document: " + new Document("$pull", lowScoreToRemove));
UpdateResult result = collection.updateOne(new Document("_id", wholeDocument.get("_id")), new BasicDBObject("$pull",
lowScoreToRemove));
System.out.println("UpdateResult: " + result + "\n");
}
}
}
}
}
iterableDocument.close();
}

以下是输出示例:

Student id: 199
Student name: Rae Kohout
Lowest Homework score is: 5.861613903793295
Document that will be updated: Document{{_id=199, name=Rae Kohout, scores=[Document{{type=exam, score=82.11742562118049}}, Document{{type=quiz, score=49.61295450928224}}, Document{{type=homework, score=28.86823689842918}}, Document{{type=homework, score=5.861613903793295}}]}}
Pull Document: Document{{$pull={ "scores" : [ { "type" : "homework" , "score" : 5.861613903793295}]}}}
UpdateResult: AcknowledgedUpdateResult{matchedCount=1, modifiedCount=0, upsertedId=null}

最佳答案

我的 mongo 版本没有 updateOne,但至少从我的“更新”经验来看,参数不应该是数组。

So instead of:
{$pull:{scores:[{type:'homework', score:5.5...}]}});
it should be (note the lack of "["):
{$pull:{scores:{type:'homework', score:5.5...}}});

无论如何,请注意已报告有关 WriteResult 的错误/不直观的功能,因此我建议还手动查看您的集合以查看已删除的内容。

关于java - MongoDB:UpdateOne 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34972687/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com