[
{
$group: {
_id: "$participantId",
documents: { $push: "$$ROOT" },
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
},
{
$unwind: "$documents"
},
{
$match: {
"documents.programCode": { $ne: "" }
}
},
{
$group: {
_id: {
participantId: "$_id",
programCode: "$documents.programCode",
programStartDate: "$documents.programStartDate"
},
baselineId: { $first: "$_id" },
documentIds: { $push: "$documents._id" },
documents: { $push: "$documents" },
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
},
{
$project: {
_id: 1,
participantId: "$_id.participantId",
programCode: "$_id.programCode",
programStartDate: "$_id.programStartDate",
baselineId: 1,
documentIds: 1
}
},
{
$lookup: {
"from": "participant",
"localField": "participantId",
"foreignField": "_id",
"as": "temp"
}
},
{
$match: {
"temp.userStatus": { $ne: "TEST" },
$and: [
{ "temp.email": { $nin: [/deleted/] } },
{ "temp": { $ne: null } }
]
}
},
{
$unwind:"$documentIds"
}
{
$project: {
_id:0
"participantId": { $arrayElemAt: ["$temp._id", 0] },
"email": { $arrayElemAt: ["$temp.email", 0] },
"documentIds": 1
}
}
]
Comments