2

I have a class that contains a List as one of the field. How can I update this field.

Found an example for updating a field

BasicDBObject newDocument3 = new BasicDBObject().append("$set", new 
    BasicDBObject().append("type", "dedicated server"));

collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3);

From link -> http://www.mkyong.com/mongodb/java-mongodb-update-document/

So this is what I have tried

BasicDBObject objectUpdateCommand = new BasicDBObject("$push", new 
    BasicDBObject("someList", stringValue));

collection.update(new BasicDBObject().append("id", user.getId()).append("email", 
    user.getEmail()), objectUpdateCommand);

Result: No change.

What am I missing?

Tried on shell and it worked [I know its not matching all the ids but it works for test purpose]

db.user.update( { Id: 'yourid'}, {$push: { someList: 'appendNewValue'} } )
6
  • I was learning how to edit my post and I appreciate you did that for me. Thanks. Commented Dec 3, 2012 at 8:26
  • have you tried the same query in mongo shell? Commented Dec 3, 2012 at 8:54
  • I have not tried that but I have seen examples of that on shell. Will give it a shot. If there is a success will let you know. Commented Dec 3, 2012 at 10:40
  • I tried the shell command and it worked. Updated the main body with the shell command. Commented Dec 3, 2012 at 11:16
  • check if the selector part is ok (maybe it's _id instead of id Commented Dec 3, 2012 at 11:35

1 Answer 1

2

I have inserted the following rows into the collection.

{ "_id" : ObjectId("50bc89ef88555f5ad35da8ba"), "id" : 1, "email" : "[email protected]", "list" : [ "list1", "list2" ] }
{ "_id" : ObjectId("50bc89f788555f5ad35da8bb"), "id" : 2, "email" : "[email protected]", "list" : [ "list1", "list2" ] }

Then by using the following code i am able to update document with id=1.

BasicDBObject cmd = new BasicDBObject().append("$push", new  BasicDBObject("list", "list3"));
coll.update(new BasicDBObject().append("id", 1).append("email","[email protected]"), cmd);

After the update rows look like :

{ "_id" : ObjectId("50bc89ef88555f5ad35da8ba"), "id" : 2, "email" : "[email protected]", "list" : [ "list1", "list2" ] }
{ "_id" : ObjectId("50bc89f788555f5ad35da8bb"), "email" : "[email protected]", "id" : 1, "list" : [ "list1", "list2", "list3" ] }

Check your code again. It should work with this code.

Sign up to request clarification or add additional context in comments.

3 Comments

Nope. That did not help. Result still the same.
@parvin both forms you posted are equivalent. The BasicDBObject(String, BasicDBObject) constructor exists for convenience.
Parvin thanks for your response. I did learn few things and your response validated I was on right track. It seems the field name was incorrect and after fixing it... everything worked smooth.

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.