Aggregate -> grouping -> grouped objects -> push only unique objects based on id

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)

Hi @Deepa_John ,

I might not understand your use case fully, but how about using the following operartor called $addToSet which should not duplicate elements.

[{$match: { workflowId: ObjectId(workflowId) }},
{$group: {
 _id: '$stageName',
 particles: {
  $addToSet: {
   id: '$particle.id'
  }
 }
}}]

Since you mentioned that you care about the parcticle.id I have only, I have specifically used its value to form the “push”…

Thanks,
Pavel