title: How to append existing array in existing collection in mongodb using java with new values
body: <p>I have a mongo collection like:</p>
  
  <pre><code>{
      "_id": ObjectId("55cad746aed75601b4822cc9"),
      "entityId": "12",
      "entityType": "a",
      "nameIdentity": [{
          "fName": "abc",
          "lName": "def",
          "dob": "00",
          "address": "xyz"
      },
  
      ]
  }
  </code></pre>
  
  <p>I am using <code>mongodb java 3.0</code> driver and trying to match and update. For eg: I am trying to match on <code>entityId</code> if it found then add the new <code>nameIdentity</code>.</p>
  
  <p>Second time when I pass </p>
  
  <pre><code>{
      "fName": "123",
      "lName": "456",
      "dob": "00",
      "address": "789"
  }
  </code></pre>
  
  <p>For my <code>entityId: 12</code> if it matches then my new collection should like this:</p>
  
  <pre><code>{
      "_id": ObjectId("55cad746aed75601b4822cc9"),
      "entityId": "12",
      "entityType": "a",
      "nameIdentity": [{
      "fName": "abc",
      "lName": "def",
      "dob": "00",
      "address": "xyz"
      }, {
      "fName": "123",
      "lName": "456",
      "dob": "00",
      "address": "789"
      }]
  }
  </code></pre>
  
  <p>I want to add it in the same matched object or collection. But its replacing the previous array and adding new like this:</p>
  
  <pre><code>{
      "_id": ObjectId("55cad746aed75601b4822cc9"),
      "entityId": "12",
      "entityType": "a",
      "nameIdentity": [
  
      {
          "fName": "123",
          "lName": "456",
          "dob": "00",
          "address": "789"
      }
      ]
  }
  </code></pre>
  
  <p>When entity id is matched I want everything to be added not updated. The code I tried is : </p>
  
  <pre><code>mongoDatabase.getCollection("entity").findOneAndUpdate(
      updateDocument, new Document("$set",entityDocument));
  </code></pre>
  
  <p>I tried with <code>$push</code> and <code>$set</code>. Its creating a new <code>nameIdentity</code> Array. But I want to add in same matched <code>nameIdentity</code> array. Any suggestions where am I going wrong ?</p>