Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating an array in MongoDB using Java driver

Tags:

I'm using MongoDB with the official Java driver (version 2.6.3). I have a MongoDB collection that contains shopping lists. A shopping list has the format

{ "_id" : { "$oid" : "4e2af1f43f8de96494d5271d"} ,
  "name" : "default" ,
  "items" : [ { "description" : "Cheese" , "quantity" : 1 , "unit" : "kg"} ,
              { "description" : "Water" , "quantity" : 3 , "unit" : "bottle"} ] }

Now I want to add a new item to the list with the update()method of DBCollection. But whatever I try it won't work although it's telling me

{ "updatedExisting" : true , "n" : 1 , "connectionId" : 63 , "err" :  null  , "ok" : 1.0}

My code does the following:

    BasicDBObject updateQuery = new BasicDBObject();
    updateQuery.put( "name", "default" );

    BasicDBObject updateCommand = new BasicDBObject();
    updateCommand.put( "$push", new BasicDBObject( "items", newShoppingItem ) );
    WriteResult result = shoppingLists.update( updateQuery, updateCommand, true, true );

newShoppingItem is a BasicDBObject which contains the data for the new item. I also tried to create the update() parameters with BasicDBObjectBuilder and JSON.parse() but it makes no difference.

I also had a look at other posts, tried googleing, but to no avail. What am I doing wrong?

Thanks for any help!
Oliver

like image 531
Baldewin Avatar asked Jul 23 '11 19:07

Baldewin


People also ask

How do you update an array element in MongoDB Java?

To update array elements that match a filter, use the filtered positional $[<identifier>] operator. You must include an array filter in your update operation to specify which array elements to update. The <identifier> is the name you give your array filter.

How do you update an array in Java?

To update or set an element or object at a given index of Java ArrayList, use ArrayList. set() method. ArrayList. set(index, element) method updates the element of ArrayList at specified index with given element.

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.


1 Answers

yes, the above code works perfectly fine. I know now where my error was. I wanted to do it bullet-proof, so I thought it would be best to use save() on the DBCollection at the end and explicitly save the shopping list DBObject:

shoppingLists.save( shoppingList );

I now read in some other forum that the objects you retrieve from the database are then not synched with the database afterwards (sounds kind of logical to me now :) ). So I overwrote the changes myself every time. After removing the line above it worked :)

So one important rule: When you update your DBCollection – this is sent directly to the database! – don't save a DBObject that you queried before the update! It will overwrite your update!

like image 129
Baldewin Avatar answered Oct 21 '22 17:10

Baldewin