Unable to update Inner Arraylist object using Mongodb Java Driver

拈花ヽ惹草 提交于 2020-01-06 14:14:31

问题


I have below document structure in mongodb database:

{
    "_id" : ObjectId("52ec7b43e4b048cd48499b35"),
    "eidlist" : [
        {
            "eid" : "64286",
            "dst" : NumberLong(21044),
            "score" : 0
        },
        {
            "eid" : "65077",
            "dst" : NumberLong(21044),
            "score" : 0
        }
    ],
    "src" : NumberLong(21047)
}

I would like to update score field of first object using Java-mongodb driver: I tried following code but it is not working :( :

  DBObject update_query=new BasicDBObject("src", key).append("eidlist.eid", e.getEdgeid());
  DBObject data=new BasicDBObject("$set",new BasicDBObject("eidlist.score",100));
 coll.update(update_query, data);

Please help me to solve this problem..I have checked all the parameter which I have passed to update function.I think something wrong with the update logic :(


回答1:


You were close. You omiited the positional operator from the update. Edit your code as shown.

DBObject data=new BasicDBObject("$set",new BasicDBObject("eidlist.$.score",100));



回答2:


Solution for this problem is:

DBObject data=new BasicDBObject("$set",new BasicDBObject("eidlist.$.score",""+100));

Ensure data type of every field used in the update query.It should be compatible with what we have stored in the mongodb :)



来源:https://stackoverflow.com/questions/21502620/unable-to-update-inner-arraylist-object-using-mongodb-java-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!