title: java mongo arraylist save
body: <p>We are using a Java server and Mongo DB [plain Java-Mongo and not Morphia or some such tool for the CRUD].<br>
  We are having a Image Pojo class and its related Metadata like below,</p>
  
  <pre><code>public class Img{
      private String name;
      private List&lt;Metadata&gt; imgMetaList = new ArrayList&lt;Metadata&gt;();
  
      //Getters, setters etc...
      public List&lt;Metadata&gt; getImgMetaList() {        
          return imgMetaList;
      }
  }
  </code></pre>
  
  <p>Metadata class has some data, implementing Serializable didn't work, so<br>
  i extended ReflectionDBObject,</p>
  
  <pre><code>public class Metadata extends ReflectionDBObject{
      private String tag;
      private String val;
  
      //Getters, setters etc...      
  }
  </code></pre>
  
  <p>I want to save the Img into Mongo. I used the following code and it worked.</p>
  
  <pre><code>BasicDBObject updateQuery = new BasicDBObject();
  updateQuery.put("name", img.getName());
  BasicDBObject updMetadata = new BasicDBObject();
  updMetadata.put("$push", new BasicDBObject("imgMetaList",img.getImgMetaList()) );
  collection.update(updateQuery,updMetadata, true, false);    
  </code></pre>
  
  <p>This inserts a document in Mongo as below,</p>
  
  <pre><code>{
      "_id" : ObjectId("503a1991db2e9f431cf0d162"),
      "name" : "test.jpg",
      "imgMetaList" : [
          [
              {
                      "Tag" : "tag1",
                      "Val" : "val1",
                      "_id" : null
              }
          ]
      ]
  }
  </code></pre>
  
  <p>Here there are 2 issues,<br>
  1. The code is inserting two square brackets instead of one to house the array<br>
  2. why isn't the _id getting generated for the list.  </p>
  
  <p>Please let me know.<br>
  Regards, Vish</p>