简体   繁体   中英

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:

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 :

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

However, as @JohnnyHK said, this isn't really a good way of doing a $where , if there is at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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