I’m having two collections contacts and invoice, All the invoices have contactID reference so that I can get a report on the list of invoices associated to each contact. The problem I 'am facing now is,
I need to merge two contacts for example ContactA and ContactB , after merge ContactB will be deleted/deactivated everything associated with ContactB will now has to point ContactA .
contacts
{
"_id" : ObjectId("5bf26f9a52d5ab002a7c44ae"),
"email" : [
"contacts@john.doe"
],
"status" : "active",
"displayName" : "contact A",
"legalName" : "contact A"
},
{
"_id" : ObjectId("5e91abecbe4a20002cc760ac"),
"email" : [
"contacts@richard.roe"
],
"status" : "active",
"displayName" : "contact B",
"legalName" : "contact B"
}
Invoice
{
"_id" : ObjectId("5e8f9cb7be4a20002cc7539a"),
"payee" : ObjectId("5bf26f9a52d5ab002a7c44ae"), // ContactA Ref
"reference_no" : "88023",
"income_date" : ISODate("2020-03-02T00:00:00.000Z"),
"due_date" : ISODate("2020-03-17T00:00:00.000Z"),
"transactions" : 1000
},
{
"_id" : ObjectId("5e8f9cb7be4a20002cc7539a"),
"payee" : ObjectId("5e91abecbe4a20002cc760ac"), // ContactB Ref
"reference_no" : "88023",
"income_date" : ISODate("2020-03-02T00:00:00.000Z"),
"due_date" : ISODate("2020-03-17T00:00:00.000Z"),
"transactions" : 1000
}
currently I have two option
(i) update all the invoice with ContactB reference with ContactA
Invoice after update
{
"_id" : ObjectId("5e8f9cb7be4a20002cc7539a"),
"payee" : ObjectId("5bf26f9a52d5ab002a7c44ae"), // ContactB Ref updated to ContactA Ref
"reference_no" : "88023",
"income_date" : ISODate("2020-03-02T00:00:00.000Z"),
"due_date" : ISODate("2020-03-17T00:00:00.000Z"),
"transactions" : 1000
}
(ii) update contact to have an array of objectID as primary reference, which has ID of both contactA and contactB.
contactA after update
{
"_id" : ObjectId("5bf26f9a52d5ab002a7c44ae"),
"email" : [
"contacts@john.doe"
],
"status" : "active",
"displayName" : "John Doe",
"legalName" : "John Doe"
"referenceId" : [ ObjectId("5bf26f9a52d5ab002a7c44ae") ,ObjectId("5e91abecbe4a20002cc760ac") ]
}
which one do you think would be an optimal approach; or do you have any alternate suggestions.