Via the aggregation pipeline on 2 collections (users and rankings) I have 3 object arrays: players, user_players and user_challengers. User_player and challengers are arrays of objects with 2 fields: name and ObjectId:
Players array matches the players to the correct challengers by ObjectId.
You can see from the screenshots that an additional problem may be that the first object in the challengers array (correctly) has name: “Hary”. Hary is a challenger, but, of course, he is Jon’s challenger and Jon is the second object in the challengers array (so there needs to be a matching of the correct id’s, not just e.g. a merging of 2 arrays).
I want a single array of corresponding names and a rank. In other words, I need to match the ObjectIds in Players to the corresponding ObjectIds and names in the other 2 arrays to give me json output that should look something like:
[
{
"matches": [
{
"player": "Hary", "challenger": "Jon", "rank": 1
},
{
"player": "Jon", "challenger": "Hary", "rank": 2
},
{
"player": "Birt", "challenger": "null", "rank": 3
}
]
}
]
There is likely a more efficient way to do this, but for now this was the only way I could find to connect the player and challenger ids to their names in the ‘users’ collection as I’m not an experienced db user.
How can I obtain the json output I’m looking for from the 3 arrays and/or is there a better, more efficient, way to achieve the same result? thanks…