I have a mongo collection like:
{ "_id": ObjectId("55cad746aed75601b4822cc9"), "entityId": "12", "entityType": "a", "nameIdentity": [{ "fName": "abc", "lName": "def", "dob": "00", "address": "xyz" }, ] }
I am using mongodb java 3.0 driver and trying to match and update. For example: I try to match on entityId if it finds, then add a new nameIdentity .
The second time I go through
{ "fName": "123", "lName": "456", "dob": "00", "address": "789" }
For my entityId: 12 , if it matches, my new collection should look like this:
{ "_id": ObjectId("55cad746aed75601b4822cc9"), "entityId": "12", "entityType": "a", "nameIdentity": [{ "fName": "abc", "lName": "def", "dob": "00", "address": "xyz" }, { "fName": "123", "lName": "456", "dob": "00", "address": "789" }] }
I want to add it to the same consistent object or collection. But its replacing the previous array and adding a new type:
{ "_id": ObjectId("55cad746aed75601b4822cc9"), "entityId": "12", "entityType": "a", "nameIdentity": [ { "fName": "123", "lName": "456", "dob": "00", "address": "789" } ] }
When the object identifier is matched, I want everything to be added not to be updated. The code I tried is:
mongoDatabase.getCollection("entity").findOneAndUpdate( updateDocument, new Document("$set",entityDocument));
I tried with $push and $set . Creates a new nameIdentity array. But I want to add the same nameIdentity array. Any suggestions I'm wrong about?