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
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) {