Hi,
I have been trying to merge multiple documents to a single document based on a unique id present on all these docs of a collection. I have tried using group, $mergeObjects , $set however I m still not able to achieve the expected output, didnt find any similar question on the internet as well so can someone help to solve this?
In below sample documents of the collection, need to merge documents having same “UniqueID” and the value in same field names of the documents getting merged should be decided conditionally.
{"_id":1,
"UniqueID":"111",
"Search_Criteria":"NameOfOrg",
"Searched_value":"IBM",
"UserNavPage_YN":"Y",
"PDF_DL":"N",
"Excel_DL":"Y",
"Id_of_entity":"121212",
"ProcessedYN":"N",
},
{"_id":2,
"UniqueID":"111",
"Search_Criteria":"NameOfOrg",
"Searched_value":"IBM",
"UserNavPage_YN":"Y",
"PDF_DL":"Y",
"Excel_DL":"N",
"Id_of_entity":"121212",
"ProcessedYN":"N",
},
{"_id":3,
"UniqueID":"222",
"Search_Criteria":"NameOfOrg",
"Searched_value":"Tesla",
"UserNavPage_YN":"Y",
"PDF_DL":"N",
"Excel_DL":"N",
"Id_of_entity":"2121",
"ProcessedYN":"N",
},
{"_id":4,
"UniqueID":"222",
"Search_Criteria":"NameOfOrg",
"Searched_value":"Tesla",
"UserNavPage_YN":"Y",
"PDF_DL":"N",
"Excel_DL":"Y",
"Id_of_entity":"2121",
"ProcessedYN":"N",
}
Expected Output:
The two docs with UniqueID = 111, should be merged to single doc as below:
{
"UniqueID":"111",
"Search_Criteria":"NameOfOrg",
"Searched_value":"IBM",
"UserNavPage_YN":"Y", // Since both docs have same value so it should remain Y
"PDF_DL":"Y", // if any doc with same uniqueId contains Y in this field then keep this as Y
"Excel_DL":"Y", // if any doc with same uniqueId contains Y in this field then keep this as Y
"Id_of_entity":"121212",
"ProcessedYN":"N",
}
Same logic to be applied to the two docs with UniqueId = 222