0

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 :(

2
  • did you run a findOne with the update query just to make sure you're getting the document you expect? Commented Feb 1, 2014 at 19:52
  • @adavis: Yes I tried with findOne and this record is exist in the database. Commented Feb 2, 2014 at 7:10

2 Answers 2

2

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));
Sign up to request clarification or add additional context in comments.

2 Comments

I am sorry your solution was right. I got the problem it's in data type. In database "eidlist.$.score" field is string and I am passing integer value in the update query.. :)
Aha. I was too quick and didn't look at that either. Good pick up.
0

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.