Hi everyone
I’m new to mongo, I’m currently building my very first API with this new technology
I have a collection that looks like this
[
{
"_id": "5f7a1b6a3aedab1df33574bb",
"name": "myFirstGame",
"players": [
{
"_id": "5f7a1be79acaf41e058ccd9f",
"name": "Albert",
"points": 10
},
{
"_id": "5f7a20de87f0fc1ea309145c",
"name": "Adam",
"points": 10
},
{
"_id": "5f7a25fdcd01211fc84afd0e",
"name": "John",
"points": 10
}
],
"description": "This is a fun game to watch",
"__v": 0
}
]
And I’m trying to get the player which name is JOHN
try{
const cond = {
_id: '5f7a1b6a3aedab1df33574bb', //the id of the game
'players.name': { $eq: 'John' }, // the condition on the player
}
player = await Game.find(cond, 'players');
return player;
}catch (e) {
console.error(e);
}
What I’m expecting to get is an object like this
{
"_id": "5f7a25fdcd01211fc84afd0e",
"name": "John",
"points": 10
}
But instead I’m getting the full collection with no filter applied
Is there anyway to get an object inside a collection array?
In SQL it would be something like thise
SELECT p.name, p.points FROM players as p
where p.game_id = '5f7a1b6a3aedab1df33574bb' AND p.name = 'John'