使用Java从mongoDB中的数组中删除子文档

时间:2014-08-25 17:09:24

标签: java arrays mongodb

我正在参加MongoDB大学的辅导班。在我的一个任务中,我必须在一个子文档数组中找到最低的作业分数(如果使用不正确的语法,请道歉)并删除该子文档。到目前为止,我的代码只能删除第一个文档中最低的作业分数,它只是在那之后迭代其他199但是没有删除任何内容。我对这些东西还很陌生,所以我不确定我哪里出错了。

以下是文档首次显示的示例:

{
"_id" : 100,
"name" : "Demarcus Audette",
"scores" : [
    {
        "score" : 47.42608580155614,
        "type" : "exam"
    },
    {
        "score" : 44.83416623719906,
        "type" : "quiz"
    },
    {
        "score" : 39.01726616178844,
        "type" : "homework"
    },
    {
        "score" : 56.73927384092820,
        "type" : "homework"
    }
  ]
 }

这是我的代码

public class MongoTest
{

public static void main(String[] args) throws UnknownHostException
{
    MongoClient client = new MongoClient();
    DB db = client.getDB("school");
    DBCollection c = db.getCollection("students");

    QueryBuilder builder = new QueryBuilder().start("scores.type").is("homework");
    DBCursor cursor = c.find(builder.get());

    List<DBObject> list;


    list = cursor.toArray();
    int i = 1;

    for (DBObject dbObject : list)
    {

        System.out.println(dbObject.toString());

        String info = dbObject.toString();
        String[] infoList = info.split(",");

        double[] scoreList = new double[2];
        String[] infoList2 = infoList[7].split(":");
        String[] infoList3 = infoList[infoList.length-1].split(":");

        char[] word = infoList2[1].toCharArray();
        char[] word2 = Arrays.copyOfRange(word, 0, word.length-3);

        String temp = String.valueOf(word2);

        char[] word3 = infoList3[1].toCharArray();
        char[] word4 = Arrays.copyOfRange(word3, 0, word3.length-3);

        String temp2 = String.valueOf(word4);


        double num = Double.parseDouble(temp);
        double num2 = Double.parseDouble(temp2);



                System.out.println(num);
                System.out.println(num2);

                BasicDBObject match = new BasicDBObject("_id", i); //to match your direct app document
                BasicDBObject update = new BasicDBObject("score", num);
                BasicDBObject update2 = new BasicDBObject("score", num2);


                if(num>num2)
                {
                    System.out.println("The first score is higher");
                    c.update(match, new BasicDBObject("$pull", update2));
                } 
                if(num2>num)
                {
                    System.out.println("The second score is higher!");
                    c.update(match, new BasicDBObject("$pull", update));
                }

        i++;
    }


    System.out.println("\ncount: " + c.count());

}

我可以让代码正确比较分数,但后来我无法删除任何东西。 感谢任何关于我失踪的帮助。

1 个答案:

答案 0 :(得分:0)

代码中需要进行小的修正。您需要在数组中提供完整的子文档:

BasicDBObject update2 = new BasicDBObject("type",
                                          "homework").append("score", num2);
 if (num < num1) {
  System.out.println("The second score is higher");
  collection.update(match, new BasicDBObject("$pull", 
                          new BasicDBObject("scores", update1)));
}

这应该有效:)