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 eg: I am trying to match on entityId if it found then add the new nameIdentity.
Second time when I pass
{
"fName": "123",
"lName": "456",
"dob": "00",
"address": "789"
}
For my entityId: 12 if it matches then my new collection should 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 in the same matched object or collection. But its replacing the previous array and adding new like this:
{
"_id": ObjectId("55cad746aed75601b4822cc9"),
"entityId": "12",
"entityType": "a",
"nameIdentity": [
{
"fName": "123",
"lName": "456",
"dob": "00",
"address": "789"
}
]
}
When entity id is matched I want everything to be added not updated. The code I tried is :
mongoDatabase.getCollection("entity").findOneAndUpdate(
updateDocument, new Document("$set",entityDocument));
I tried with $push and $set. Its creating a new nameIdentity Array. But I want to add in same matched nameIdentity array. Any suggestions where am I going wrong ?