title: Updating an array in MongoDB using Java driver
body: <p>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</p>
  
  <pre><code>{ "_id" : { "$oid" : "4e2af1f43f8de96494d5271d"} ,
    "name" : "default" ,
    "items" : [ { "description" : "Cheese" , "quantity" : 1 , "unit" : "kg"} ,
                { "description" : "Water" , "quantity" : 3 , "unit" : "bottle"} ] }
  </code></pre>
  
  <p>Now I want to add a new item to the list with the <code>update()</code>method of <code>DBCollection</code>. But whatever I try it won't work although it's telling me</p>
  
  <pre><code>{ "updatedExisting" : true , "n" : 1 , "connectionId" : 63 , "err" :  null  , "ok" : 1.0}
  </code></pre>
  
  <p>My code does the following:</p>
  
  <pre><code>    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 );
  </code></pre>
  
  <p><code>newShoppingItem</code> is a <code>BasicDBObject</code> which contains the data for the new item. I also tried to create the <code>update()</code> parameters with <code>BasicDBObjectBuilder</code> and <code>JSON.parse()</code> but it makes no difference.</p>
  
  <p>I also had a look at other posts, tried googleing, but to no avail. What am I doing wrong?</p>
  
  <p>Thanks for any help!<br>
  Oliver</p>