Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将条件定义为变量以在mongodb中查找方法?_Mongodb_Find_Condition Variable - Fatal编程技术网

如何将条件定义为变量以在mongodb中查找方法?

如何将条件定义为变量以在mongodb中查找方法?,mongodb,find,condition-variable,Mongodb,Find,Condition Variable,在传递到“find”方法(对于MongoDB)之前,我想使用代码将“condition”作为变量进行操作: 但我无法让它工作。有人能帮我怎样才能成功吗?谢谢您能否更具体地说明您的代码,例如您是如何设置连接的,您是如何尝试使用结果的,以及在尝试运行代码时遇到了哪些错误。请尝试下面的代码 var FindQuery=function(cat){ if (cat) { var condition = { $and: [{brand: brnd},

在传递到“find”方法(对于MongoDB)之前,我想使用代码将“condition”作为变量进行操作:


但我无法让它工作。有人能帮我怎样才能成功吗?谢谢

您能否更具体地说明您的代码,例如您是如何设置连接的,您是如何尝试使用结果的,以及在尝试运行代码时遇到了哪些错误。请尝试下面的代码

    var FindQuery=function(cat){
    if (cat) {
    var condition = { $and: [{brand: brnd},
                    {category: cat} ] }  
    } else { 
    var condition = {brand: brnd} 
    }

    return productCol.find(condition);
    };
    FindQuery(condition)

嗨,Saketh,我检查您的代码是否工作(但代码的方式不同)。当我再次对代码进行测试时,我意识到我犯的是值传递错误。它在其他方面也起作用。感谢您对以上内容的更正。感谢saketh在下面的提示,上面的代码实际上是在工作,而不是我自己在值传递中的错误,它给出了不准确的结果。
    var FindQuery=function(cat){
    if (cat) {
    var condition = { $and: [{brand: brnd},
                    {category: cat} ] }  
    } else { 
    var condition = {brand: brnd} 
    }

    return productCol.find(condition);
    };
    FindQuery(condition)