简体   繁体   English

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

[英]removing a subdocument from an array in mongoDB using Java

Im taking the MongoDB University tutorial class right now. 我现在正在上MongoDB大学教程课程。 In one of my assignments I have to find the lowest homework score in an array of subdocuments (apologies if im using incorrect syntax) and remove that subdocument. 在我的一项任务中,我必须在一系列子文档中找到最低的家庭作业分数(如果即时通讯使用了错误的语法,请道歉)并删除该子文档。 So far my code has only worked to remove the lowest homework score of the first document, it just iterates over the other 199 after that but doesnt remove anything. 到目前为止,我的代码只能删除第一个文档的最低作业成绩,此后它仅遍历其他199个文档,但不删除任何内容。 I'm still pretty new to this stuff so I'm not sure where I am going wrong. 我对这些东西还很陌生,所以我不确定我要去哪里。

Here is an example of what a document first looks like: 这是文档最初的外观示例:

{
"_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"
    }
  ]
 }

Here is my code 这是我的代码

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());

}

I can get the code to correctly compare scores but then I cant get it to remove anything. 我可以获取代码以正确比较分数,但后来却无法删除任何内容。 Thanks for any help as to what im missing. 感谢您对我所缺少的任何帮助。

Small correction needed in your code. 您的代码中需要进行小的更正。 You need to provide entire sub document in the as such in the array: 您需要在数组中这样提供整个子文档:

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)));
}

This should work:) 这应该工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM