In my "equipos" database, the structure is as follows:
[
"_id" : ObjectId("5ae4ea9f434d9b51dad68813"),
"team_name" : "Alavés",
"nombre_equipo_movil" : "ALA",
"cantidad_integrantes" : 20,
"partidos_jugados" : 29,
"partidos_ganados" : 10,
"partidos_empatados" : 1,
"partidos_perdidos" : 18,
"goles_a_favor" : 26,
"goles_en_contra" : 45,
"players" : [
{
"dorsal" : 1,
"nombre_jugador" : "Fernando Pacheco",
"edad" : 25,
"nacionalidad" : "España",
"posicion" : "Portero",
"goles" : 0,
"asistencias" : 0,
"amarillas" : 4,
"rojas" : 1
}
...
]
...
]
I am trying to find out if there is a player with "dorsal" equal to 1 in the team Alavés. I have written the query as follows:
db.equipos.findOne({"team_name": "Alavés" }, {"players": {$elemMatch: {"dorsal": 1l}}})
However, the issue arises when there is no player found with dorsal 1, and the response displays the team's ID like this:
{ "_id" : ObjectId("5ae4ea9f434d9b51dad68813") }
This poses a problem for me when I try to compare the response against null using Express:
dbo.collection("equipos").findOne(
{"team_name": sReq.body.nombre_equipo }, {"players": {$elemMatch: {"dorsal": sReq.body.dorsal}}},
function(err, res){
Do you have any suggestions on how I can determine if the searched player does not exist based on this type of response?