I have game data in JSON format in my MongoDB Atlas. i have tried to retrieve all games player 1 has played in whole tournament though I’m not able to get anything returned to me even when using MongoDB Compass I was able to show all games from type played but not specific ones for getting more data after that would be easier I hope when the restrictions are made correctly
Part of one game JSON data database is holding all games
{
"num": 12033,
"name": "Team Elimination",
"type": "team",
"time": 0,
"gamePlayed": 360,
"dura": 420,
"sbtype": "STANDARD_TEAM",
"showPower": false,
"showHealth": true,
"totalPlayers": 20,
"totalTeams": 4,
"teams": [
{
"id": 1,
"name": "Blue team",
"pos": 1,
"stats": {
"score": 3730,
"totalPlayers": 5,
"totalTagsFor": 28,
"totalTagsAgainst": 20,
"totalShots": 277,
"scoreTimeLine": {
},
"isEliminated": false
},
"colour": "BLUE",
"players": [
{
"id": 21,
"alias": "player 1",
"tid": 1,
"pos": 1,
"omid": 5116714,
"stats": {
"hp": 3,
"score": 1430,
"tagson": 8,
"tagsby": 5,
"actualScore": 1430,
"tagRatio": 160,
},
},
{
"id": 30,
"alias": "player 2",
"tid": 1,
"pos": 2,
"omid": 5167552,
"stats": {
},
}
This is what I have in my code, in my backend side:
const handleSearch = async (e) => {
e.preventDefault();
try {
const response = await axios.get(`http://localhost:3001/events/${searchTerm}`);
console.log('Response:', response);
setPlayerData(response.data);
setError('');
} catch (err) {
setPlayerData([]);
console.log(err)
setError('Player not found or an error occurred');
}
};
Server side:
app.get('/players/:alias', async (request, response) => {
const alias = request.params.alias.toLowerCase();
try {
const events = await Event.find({'games.elim.teams.players.alias': alias});
console.log('pelaajan nimi', events)
const players = events.flatMap(event =>
event.games.elim.flatMap(games =>
event.games.elim.players.filter(player => player.alias.toLowerCase() === alias),
console.log(event.teams.flatMap(team => team.players))
)
);
if (events.length === 0) {
return response.status(404).json({ error: 'Player not found' });
};
// Filter to get player details
response.json(players);
} catch (error) {
console.log(error)
response.status(500).send({ error: 'Error fetching player data' });
}
});
and event schema for games in server side code.
Games are in schema that’s in
due to error wasnt able to write schema
in here we expect that all game data are in games.elim
.
The problem is that I haven’t been able to get successful return from player found either from correct “alias” and after that from all games that player with that alias has played for more data