so I have the following nested collection in a MongoDB database:
{'_id': ObjectId('615e9b8d17fa084f2e8d4b83'),
'status': 'Played',
'roundId': 4165363,
'gameweek': 2,
'teamsData': {'12274': {'scoreET': 0,
'coachId': 89543,
'side': 'home',
'teamId': 12274,
'score': 0,
'scoreP': 0,
'hasFormation': 1,
'formation': {'bench': [{'playerId': 101652,
'assists': '0',
'goals': 'null',
'ownGoals': '0',
'redCards': '0',
'yellowCards': '0'},
....
{'playerId': 90991,
'assists': '0',
'goals': 'null',
'ownGoals': '0',
'redCards': '0',
'yellowCards': '0'},
'9598': {'scoreET': 0,
'coachId': 122788,
'side': 'away',
'teamId': 9598,
'score': 3,
'scoreP': 0,
'hasFormation': 1,
'formation': {'bench': [{'playerId': 69964,
'assists': '0',
'goals': 'null',
'ownGoals': '0',
'redCards': '0',
'yellowCards': '0'},
....
'lineup': [{'playerId': 69616,
'assists': '0',
'goals': '1',
'ownGoals': '0',
'redCards': '0',
'yellowCards': '39'}
I want a list of playerId and goals and I know I can project something like
list(db.matches.find({},
projection = ['teamsData.9598.formation.lineup.playerId', 'teamsData.9598.formation.lineup.goals']))
which would bring me
[{'_id': ObjectId('615e9b8d17fa084f2e8d4b83'),
'teamsData': {'9598': {'formation': {'lineup': [{'playerId': 69616, 'goals': '1'},
{'playerId': 14812, 'goals': 'null'},
{'playerId': 69409, 'goals': 'null'},
{'playerId': 25393, 'goals': 'null'},
{'playerId': 135747, 'goals': 'null'},
{'playerId': 3476, 'goals': '1'},
{'playerId': 105361, 'goals': 'null'},
{'playerId': 8287, 'goals': '1'},
{'playerId': 69396, 'goals': 'null'},
{'playerId': 69968, 'goals': 'null'},
{'playerId': 14943, 'goals': '0'}]}}}}]
The problem is that I don't want to write down 9598 part in the project code (and then for each teamId), otherwise I won't be able to scale it through whole collection. Is there anyone know how can I take playerId and goals for all teams?
I read several questions regarding nested JSON but none in a structure like that, so I appreciate any help. Thanks :)
teamsDatawith an array, and make the key part of the embeded document. Unknown keys make data querying/updating hard