Monorepo icon indicating copy to clipboard operation
Monorepo copied to clipboard

Multiple normalized responses for same survey with same userId

Open SachaG opened this issue 1 year ago • 0 comments

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.

Screenshot 2024-11-28 at 11 26 21

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
    }
  }
]

SachaG avatar Nov 28 '24 02:11 SachaG