I've a little problem with my script. I cannot passing a parameter (MYPARAMETER in the example) in the function in the '$where'.
我的剧本有点问题。我不能在“$where”函数中传递一个参数(本例中的MYPARAMETER)。
You have an idea for help me ? Thanks.
你有什么办法帮我?谢谢。
var MYPARAMETER = "dqsd qsdqs &é&é";
MyCol.findOne({
$where: function(MYPARAMETER)
{
var tab1="ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ!#$€%&'`(),;:/@.*[]\|";
var tab2= "aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn ";
rep2=tab1.split('');
rep=tab2.split('');
parray=new Array();
var i=-1;
while(rep2[++i])
{
parray[rep2[i]]=rep[i]
}
var chaine = this.name.replace(/\s{1,}/g,"-");
chaine = chaine.replace(/./g, function($0){return (parray[$0])?parray[$0]:$0 });
chaine = chaine.replace(/\s/g,"");
return (chaine == MYPARAMETER);
}
},
3 个解决方案
#1
3
A $where
executes on the server, and thus wouldn't have access to a variable that was locally declared on another server. JavaScript should be used as a last resort generally in MongoDB, and this appears to go the other way and perform a LOT of very time consuming work.
在服务器上执行的$,因此不能访问在另一台服务器上声明的变量。在MongoDB中,JavaScript应该作为最后的解决方案,而这似乎恰恰相反,它执行了大量耗时的工作。
You'd need to build the function as a string and include the value directly.
您需要将函数构建为一个字符串,并直接包含该值。
#2
1
To use variables in $where query, first create mongo query as a string and then parse string to JSON when passing as parameter
要在$where查询中使用变量,首先创建mongo查询作为字符串,然后在传递参数时解析字符串到JSON。
Example:
例子:
var query = '{"$where": "function() { return (\'' + address + '\').indexOf(this.city) >= 0 }"}';
db.city.find(JSON.parse(query);
#3
0
This one is works for me , Just try to store a query as a string in one variable then concat your variable in query string,
这个对我来说是可行的,只要将查询作为字符串存储在一个变量中然后将变量concat保存在查询字符串中,
var local_var = 42
var local_var = 42
var query = "{$where: function() { return this.a == "+local_var+"}}"
var查询= "{$where: function(){返回这个。一个= = + local_var + } }”
db.test.find(query)
db.test.find(查询)
看参考的做法
#1
3
A $where
executes on the server, and thus wouldn't have access to a variable that was locally declared on another server. JavaScript should be used as a last resort generally in MongoDB, and this appears to go the other way and perform a LOT of very time consuming work.
在服务器上执行的$,因此不能访问在另一台服务器上声明的变量。在MongoDB中,JavaScript应该作为最后的解决方案,而这似乎恰恰相反,它执行了大量耗时的工作。
You'd need to build the function as a string and include the value directly.
您需要将函数构建为一个字符串,并直接包含该值。
#2
1
To use variables in $where query, first create mongo query as a string and then parse string to JSON when passing as parameter
要在$where查询中使用变量,首先创建mongo查询作为字符串,然后在传递参数时解析字符串到JSON。
Example:
例子:
var query = '{"$where": "function() { return (\'' + address + '\').indexOf(this.city) >= 0 }"}';
db.city.find(JSON.parse(query);
#3
0
This one is works for me , Just try to store a query as a string in one variable then concat your variable in query string,
这个对我来说是可行的,只要将查询作为字符串存储在一个变量中然后将变量concat保存在查询字符串中,
var local_var = 42
var local_var = 42
var query = "{$where: function() { return this.a == "+local_var+"}}"
var查询= "{$where: function(){返回这个。一个= = + local_var + } }”
db.test.find(query)
db.test.find(查询)
看参考的做法