C#C
C#3y ago
TeBeCo

❔ MONGO - Aggregate pipeline - GroupBy mutliple fields

I'm trying to run duplicate detection on nested object like this:

Here's 3 object in the same collection:
[{
  UserId:1,
  Badges: [
    { BadgeId:1, Email: "my.mail@foo.com", ExternalId: 111 },
    { BadgeId:2, Email: "my.mail@foo.com", ExternalId: 222 }
  ]
},
{
  UserId:2,
  Badges: [{
    { BadgeId:3, Email: "my.mail@foo.com", ExternalId: 111 }
  ]
},
{
  UserId:3,
  Badges: [{
    { BadgeId:4, Email: "my.mail@foo.com", ExternalId: 222 }
    { BadgeId:5, Email: "NOPNOP@foo.com", ExternalId: 222 }
  }],
},
{
  UserId:4,
  Badges: [{
    { BadgeId:6, Email: "my.mail@foo.com", ExternalId: 111 }
    { BadgeId:7, Email: "my.mail@foo.com", ExternalId: 111 }
    { BadgeId:8, Email: "NOPNOP@foo.com", ExternalId: 222 }
  }],
}]


Badge Duplicate detection should return, dup' Badge that have same Email
  • ExternalId
    • Badge 1, 3, 6, 7 as same badge => on user 1, 2, 4 (4 twice)
    • Badge 2 and 4 => for user 1 and 3
'm guessing $unwind will eventually helps as it will flatMap the data on ever Badges:
db.Foos.aggregate([
  { $unwind: { path: "$Badges" } }
])

but I don't know how to Group By { "Badge.Email", "Badge.ExternalId" } and associating all the user with it
Was this page helpful?