Morning Jack,
With $unwind and $group stages, I can identify the duplicates.
Is this the best way to do it?
[
{
$unwind: "$markets"
},
{
$unwind: "$markets.salePeriods"
},
{
$unwind:
"$markets.salePeriods.panel.storeSegmentations"
},
{
$group: {
_id: {
docId: "$_id",
marketId: "$markets.marketId",
salePeriodId:
"$markets.salePeriods.salePeriodId",
storeSegmentationId:
"$markets.salePeriods.panel.storeSegmentations.segmentationType"
},
count: {
$sum: 1
}
}
},
{
$match: {
count: {
$gt: 1
}
}
},
{
$group: {
_id: {
docId: "$_id.docId",
marketId: "$_id.marketId",
salePeriodId: "$_id.salePeriodId"
},
duplicateStoreSegmentations: {
$push: "$_id.storeSegmentationId"
}
}
},
{
$group: {
_id: {
docId: "$_id.docId",
marketId: "$_id.marketId"
},
duplicateSalePeriods: {
$push: {
salePeriodId: "$_id.salePeriodId",
duplicateStoreSegmentations:
"$duplicateStoreSegmentations"
}
}
}
},
{
$group: {
_id: "$_id.docId",
duplicateMarkets: {
$push: {
marketId: "$_id.marketId",
duplicateSalePeriods:
"$duplicateSalePeriods"
}
}
}
}
]