MongoDB:UpdateOne Record

时间:2016-01-24 05:48:31

标签: java mongodb mongodb-query pull

我已经编写了以下程序,通过删除最低成绩来更新设置记录。根据程序的输出,似乎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}

2 个答案:

答案 0 :(得分:1)

我的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的报告错误/不直观的功能,因此我建议您手动查看您的收藏集以查看已删除的内容。

答案 1 :(得分:0)

请参阅注释掉BasicDBList的代码行。

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", new BasicDBObject("type", "homework").append("score", homeworkScore1));

                            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));
                            //iterable.
                            System.out.println("UpdateResult: " + result + "\n");
                        }
                    }
                }
            }
        }
        iterableDocument.close();
    }