更新mongodb java中的嵌套数组值

时间:2016-11-10 14:09:49

标签: java mongodb

这是我的database structure

 {
"_id" : ObjectId("58243c430386650d78d12d53"),
"email" : "technotest91@gmail.com",
"dentistName" : "Suzuka",
"tempDetails" : [ 
    {
        "tempName" : "Elsa",
        "city" : "bangalore",
         "tempEmail": "abc@gmail.com",
        "experience" : "5",    
        "hiredDates" : [ 
            {
                "status" : "Accepted",
                "bookingId" : "36Y0YM",
            }
        ],
    }, 
    {
        "tempName" : "Elsa",  
        "city" : "bangalore",
         "tempEmail": "abc@gmail.com",
        "experience" : "5",   
        "hiredDates" : [ 
            {

                "status" : "Hired",
                "bookingId" : "92Qhd7",
            }
        ],  
    }, 
    {
        "tempName" : "Elsa",  
        "city" : "bangalore",
        "experience" : "5",
        "tempEmail": "abc@gmail.com",
        "typeOfPractice" : "Oral Surgery",
        "hiredDates" : [ 
            {
                "status" : "Hired",
                "bookingId" : "95kTe9",
            }
        ],
    }
]
 }

我正在尝试根据Status更新bookingId。我无法更新该值。我在JAVA MONGODB中尝试了以下代码,但似乎没有更新。

    BasicDBObject searchQuery = new BasicDBObject();

    List<BasicDBObject> andQuery = new ArrayList<BasicDBObject>();
    andQuery.add(new BasicDBObject("email", dentalEmail));
    andQuery.add(new BasicDBObject("tempDetails.tempEmail",tempEmail));


    searchQuery.put("$and", andQuery);

    DBCursor cursor = col.find(searchQuery);
     if (cursor.count() != 0) {
    while (cursor.hasNext()) {

        BasicDBList jobStatusList = (BasicDBList) cursor.next().get("tempDetails");

            for(int k=0;k<jobStatusList.size();k++)
            {
                BasicDBObject tempJobObject = (BasicDBObject) jobStatusList.get(k);

                BasicDBList hiredDatesList = (BasicDBList) tempJobObject.get("hiredDates");


                for(int i =0; i<hiredDatesList.size();i++)
                {
                    BasicDBObject hiredDatesObject = (BasicDBObject) hiredDatesList.get(i);
                    String dbBookingId = hiredDatesObject.getString("bookingId");

                    if(dbBookingId.equalsIgnoreCase(bookingId))
                    {
                        BasicDBObject doc = new BasicDBObject("$set",
                                new BasicDBObject().append("tempDetails."+k+".hiredDates."+i+".status", status));

                        col.update(searchQuery, doc, false, false);

                        System.out.println(doc);

                    }else{
                        result = false;
                    }
                }

请帮助我解决这个问题,因为我一直在努力根据BookingId更新记录。我通过查询获取文档并迭代对象,如果bookingid匹配,我正在更新记录。请帮助告诉我应该改变的内容..

1 个答案:

答案 0 :(得分:1)

您是否正确检查了数据库?我能够完美地运行你的代码而没有错误,它也会更新集合。

我刚创建了一个方法,我正在执行以下操作:

private void performOperation() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoClient mongoClient = new MongoClient();
    DB db = mongoClient.getDB("test");
    DBCollection table = db.getCollection("vinay");

    logger.info("connected successfully");

    BasicDBObject searchQuery = new BasicDBObject();

    List<BasicDBObject> andQuery = new ArrayList<BasicDBObject>();
    andQuery.add(new BasicDBObject("email", "technotest91@gmail.com"));
    andQuery.add(new BasicDBObject("tempDetails.tempEmail", "abc@gmail.com"));

    searchQuery.put("$and", andQuery);

    logger.info("executing :"+ searchQuery);

    DBCursor cursor = table.find(searchQuery);
    if (cursor.count() != 0) {
        while (cursor.hasNext()) {

            BasicDBList jobStatusList = (BasicDBList) cursor.next().get("tempDetails");

            for (int k = 0; k < jobStatusList.size(); k++) {
                BasicDBObject tempJobObject = (BasicDBObject) jobStatusList.get(k);

                BasicDBList hiredDatesList = (BasicDBList) tempJobObject.get("hiredDates");

                for (int i = 0; i < hiredDatesList.size(); i++) {
                    BasicDBObject hiredDatesObject = (BasicDBObject) hiredDatesList.get(i);
                    String dbBookingId = hiredDatesObject.getString("bookingId");

                    if (dbBookingId.equalsIgnoreCase("36Y0YM")) {
                        BasicDBObject doc = new BasicDBObject("$set", new BasicDBObject()
                                .append("tempDetails." + k + ".hiredDates." + i + ".status", "New Status"));

                        table.update(searchQuery, doc, false, false);

                        logger.info(doc);

                    } else {
                        logger.info("no record find");
                    }
                }
            }
        }
    }
}

在我执行此状态后,预订ID为36YYYM的状态更新为 - &#34;新状态&#34;。

但我发现了一个。你编写循环的方式...因为即使在记录更新后它再次迭代并打印消息&#34;没有找到记录&#34;在你的情况下,它会将结果设置为false,所以如果你依赖于结果,那么你的整个方法都是错误的。

我不知道你正在尝试构建什么逻辑但重点是你的代码中没有任何错误,如果你写的循环有任何错误,你可以更新mongodb中的记录。