I have a mongo collection which has many documents containing information about credits / gifts given to our users.
I would like to be able to run an Aggregated Query to get all the users who have received a credit / gift within the last x number of days and sum the total value of those credits for each user.
I think I have the correct Aggregation, group and sum code but I'm getting back more than one object per user, which shouldn't be the case since I'm grouping and then summing the values(?)
@Getter
@Setter
public class RecentlyRewardedUsers {
@Field("userId")
private String userId;
private String totalValueAwarded;
private String userDisplayName;
private String profilePicUrl;
Like I said above, when the the results of the query are mapped to the RecentlyRewardedUsers class, I see multiple entries for the same userId (see image below). I would have thought it should be all rolled up / summed up into one entry for that userId
If anyone can shed some light on what I've done wrong that would be great!!
Thank you
** EDIT **
Based on the answer below from
user Eskandar Abedini, I've updated my code to be the following:
Adding image to show what I mean by userId not being mapped in the MappedResponse. However in the Raw response of the query the userId is getting mapped to the documentId _id
Answers
Remove _id from group("userId", "_id")
This is somehow similar to yours, its implementation is clear too.