Monorepo
Monorepo copied to clipboard
Multiple normalized responses for same survey with same userId
I noticed that for some older surveys we seem to have multiple copies of the same survey for the same user, which should in theory not be possible.
It's probably some bug due to importing those responses from a previous system, but I thought I'd make a note of it.
Aggregation used:
[
{
// Filter documents where both userId and editionId exist
$match: {
userId: {
$exists: true,
$ne: null
},
editionId: {
$exists: true,
$ne: null
}
}
},
{
// Group by userId and editionId
$group: {
_id: {
userId: "$userId",
editionId: "$editionId"
},
count: {
$sum: 1
} // Count the documents in each group
}
},
{
// Optional: Sort by count in descending order
$sort: {
count: -1
}
},
{
// Optional: Project the fields for better readability
$project: {
_id: 0,
// Hide the _id field
userId: "$_id.userId",
editionId: "$_id.editionId",
count: 1
}
}
]