Angularjs 无法使用函数(req、res、next、id)mongoose(mongodb)中的动态id字段进行查询
我使用的是mean js,我试图通过angularjs中的服务调用查询mongodbAngularjs 无法使用函数(req、res、next、id)mongoose(mongodb)中的动态id字段进行查询,angularjs,mongodb,mongoose,mongodb-query,mean-stack,Angularjs,Mongodb,Mongoose,Mongodb Query,Mean Stack,我使用的是mean js,我试图通过angularjs中的服务调用查询mongodb var Priority = 'Left_VM_P'; url = currentUrl+"/api/queryPrioritySearch/"+Priority; 该查询仅在以下情况下有效: Property.find({ Left_VM_P : true }).exec(function(err, properties) { 当我尝试用id的值替换变量Left_VM_p时,它没有响应
var Priority = 'Left_VM_P';
url = currentUrl+"/api/queryPrioritySearch/"+Priority;
该查询仅在以下情况下有效:
Property.find({ Left_VM_P : true }).exec(function(err, properties) {
当我尝试用id的值替换变量Left_VM_p时,它没有响应
exports.queryPrioritySearch = function(req, res, next, id) {
console.log('id = ', id);
Property.find({ id : true }).exec(function(err, properties) {
在控制台日志记录时
id的值是
id = Left_VM_P
这是mongo对象的示例
{
"Left_VM_P": true,
"Later_P": true,
"High_P": true,
"last_date_call_was_made": "-",
"call_priority": "-"
}
此外,当我在mongo shell中搜索时,它会正确返回值
> db.properties.find({"Left_VM_P" : true}).count();
3
我认为您不能将变量放在mongodb查询的左侧。MangGDB总是把左侧作为字段名作为字符串。所以
Property.find({ Left_VM_P : true }).exec(function(err, properties)
会起作用,因为它把左撇子vMyp当作字符串,所以它像<代码>“LeftTyVMpP”:Trime< /Calp>
如果是Property.find({id:true})
则将其作为字符串“id:true
但是您希望用动态名称代替id,以便可以尝试此解决方案
var dynamicId={};
dynamicId[id]=true;
Property.find(dynamicId).exec(function(err, properties)
我希望这会有所帮助:)
以下是完整版本:
exports.queryPrioritySearch = function(req, res, next, id) {
var id_2 = id;
var dynamicId={};
dynamicId[id_2]=true;
Property.find(dynamicId).exec(function(err, properties) {