Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

278
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!