简体   繁体   English

可以在MongoDB $ where子句中使用函数吗?

[英]Is it possible to use function in MongoDB $where clause?

I need to find out that if it is possible to use function in $where clause string like this: 我需要找出是否可以在$ where子句字符串中使用函数,如下所示:

db.myCollection.find( "this.name == 'xyz' || function() {return 1>0;} || this.name == 'abc'" );

Thanks in advance. 提前致谢。

I'm not quite sure what you're trying to do, but put all your checks in a single function that's the value of the $where : 我不太确定您要执行的操作,但是将所有检查都放在$where值的单个函数$where

db.myCollection.find({$where: function() {
    return this.name == 'xyz' || 1>0 || this.name == 'abc';
}});

It is possible to use nested functions within functions in JS but there are some considerations as this question will tell you: JavaScript Nested function 可以在JS的函数中使用嵌套函数,但是有一些注意事项,因为此问题将告诉您: JavaScript嵌套函数

However, as @JohnnyHK said, this isn't really a good way of doing a $where , if there is at all. 但是,正如@JohnnyHK所说,如果有的话,这并不是在$where上做的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM