Example of my schema / document:
{
"_id": {
"$oid": "61f433cc3d5605c535523dbf"
},
"particle": {
"particleId": {
"$oid": "61f433cb3d5605c535523a70"
},
"id": "db01ed2983a75a84973f281eef4afe98c614ab66",
},
"stageName": "TO DO",
"workflowId": {
"$oid": "61f433c182824700196f6893"
}
}
This is my query:
const pipeline = [
$match: { workflowId: ObjectId(workflowId) },
$group: {
_id: '$stageName',
particles: { $push: '$particle' }
}
]
const cursor = await collection.aggregate(pipeline)
For a specific workflow, I am grouping by stageNames and pushing the documents. The particle.id can be duplicate. I do not want to push duplicate documents. Two docs are duplicates if particle.id and stageName are same for both of them.
I have a compound index on the following:
workflowId(ascending) - stageNameparticle(ascending) - particleId(ascending)