Arai Arai - 24 days ago 8
Node.js Question

How to find a document using nested value MongoDB

I have a Json like this:

"datos_personales":
{
"nombre":"Dionel",
"apellido":"Delgado",
"fechanacimiento":"1990-12-31T04:00:00.000Z",
"lugarNacimiento":"Venezuela, Maracaibo",
"edad":25,
"genero":"Masculino",
"cedula":"21076754",
"direccion":"San Carlos",
"telefonofijo":"0262-6871111",
"telefonomovil":"0262-6871111"
},

"datos_emergencia":
{
"nombre1":"Jeaynie",
"apellido1":"Valbuena",
"telefono1":"0262-6871111",
"telefono2":"0262-6871111",
"parentesco1":"Madre",
"nombre2":"Diones",
"apellido2":"Delgado",
"telefono3":"0262-6871111",
"telefono4":"0262-6871111",
"parentesco2":"Padre"
},

"datos_sociales":
{
"civil":"Soltero",
"estudios4":true,
"ocupacion":"Programador Web",
"hijos":"No"
},
"datos_medicotratante":
{
"nombre":"Naikelin",
"apellido":"Ruiz",
"telefono1":"0262-6871111",
"telefono2":"0262-6871111",
"especialidad":"PediatrĂ­a",
"sangre":"AB",
"rh":"Negativo",
"seguro":"No"
}


`

I need to query these kinds of documents with nodeJS using "cedula" to find them

{"datos_personales.cedula":21076754}
<< in this case

I tried using this but it doesn't fetch the document

var ced = 21076754;
db.getCollection('users').find({"datos_personales.cedula":ced});


any suggestions?

Answer
"cedula":"21076754",

Your result is a string (See the quotes).

Change your query to:

var ced = "21076754";
db.getCollection('users').find({"datos_personales.cedula":ced});
Comments