Skip to content
Advertisement

join object from another documnt with localfield key

i have a competition doc with field teams array of object with _id of team and a score doc with teamId field

competitions.teams = [{_id: 100,..}, {..}] score.teamId = 100

when aggregatig score i want to group it to the competition teams but imm getting all team inside the group innstead of matching id

sample document https://mongoplayground.net/p/yJ34IBnnuf5

JavaScript

returns all team in group instead of matched teamid

JavaScript

this is the result im trying to accomplish please guide me

JavaScript

what should i do i’ve read the docs this seems to be the way?

Advertisement

Answer

Demo – https://mongoplayground.net/p/ETeroLftcZZ

You have to add $unwind: { "path": "$comp.teams" } and after that group by { $group: { "_id": "$comp.teams._id" ... }

JavaScript

Demo with more data – https://mongoplayground.net/p/b41Ch5ge2Wp

Advertisement