Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
Unable to query using the dynamic id field in function(req, res, next, id) mongoose ( mongodb )

I'm using mean js, I'm trying to query mongodb via a Service call in angularjs,

   var Priority = 'Left_VM_P'; 
   url = currentUrl+"/api/queryPrioritySearch/"+Priority;

the query works only when

Property.find({ Left_VM_P : true }).exec(function(err, properties) {  

when i try to replace the variable Left_VM_P with the value of the id, it doesn't respond.

exports.queryPrioritySearch = function(req, res, next, id) { 
console.log('id = ', id); 
Property.find({ id : true }).exec(function(err, properties) {           

Upon console logging the value of id comes to be

id =  Left_VM_P

Here's the sample mongo object.

    { 
      "Left_VM_P": true,
      "Later_P": true,
      "High_P": true,
      "last_date_call_was_made": "-",
      "call_priority": "-"
    }

Also when I search in the mongo shell it returns value correctly.

> db.properties.find({"Left_VM_P" : true}).count();
3
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

i think you can not put variable on left side of mongodb query. mongodb always consider left side as field name as string. so

Property.find({ Left_VM_P : true }).exec(function(err, properties) 

will work because it consider left_VM_P as string so it is like "Left_VM_P": true

in case of Property.find({ id : true }) it is taking it as string "id":true

but you want dynamic name in place of id so you can try this solution

var dynamicId={};
dynamicId[id]=true;
Property.find(dynamicId).exec(function(err, properties)

i hope this will help :)

Here's the full version:

exports.queryPrioritySearch = function(req, res, next, id) { 
var id_2  = id; 

var dynamicId={};
dynamicId[id_2]=true;


Property.find(dynamicId).exec(function(err, properties) { 
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda