Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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/2/node.js/38.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
Javascript MongoDb-使用db.system.js中存储的函数时,$where的性能更高吗?_Javascript_Node.js_Performance_Mongodb - Fatal编程技术网

Javascript MongoDb-使用db.system.js中存储的函数时,$where的性能更高吗?

Javascript MongoDb-使用db.system.js中存储的函数时,$where的性能更高吗?,javascript,node.js,performance,mongodb,Javascript,Node.js,Performance,Mongodb,MongoDb建议“通常,只有在无法使用其他运算符表达查询时,才应使用$where”,因为性能原因。但是,我们似乎可以使用特殊的表system.js在服务器端存储Javascript函数 因此,与其这样做,不如: db.myCollection.find( { $where: function() { return this.credits == this.debits; } } ); 改为这样做: db.system.js.save({ _id : 'queryFunction` , val

MongoDb建议“通常,只有在无法使用其他运算符表达查询时,才应使用$where”,因为性能原因。但是,我们似乎可以使用特殊的表
system.js
在服务器端存储Javascript函数

因此,与其这样做,不如:

db.myCollection.find( { $where: function() { return this.credits == this.debits; } } );
改为这样做:

db.system.js.save({ _id : 'queryFunction` , value : function() { return this.credits == this.debits; } });
...
db.myCollection.find( { $where: 'queryFunction' ); //not sure about syntax
第二种变体是否会获得任何性能优势;如果是这样,它们是否重要


参考资料:

    • 这没用


      在任何情况下,该函数都将作为字符串加载,并在MongodbsJavaScript引擎为每个单个文档执行之前编译到javascript函数。无论您是通过网络还是通过对
      system.js
      的查询来加载它,都不会有太大的区别,即使是这样,在大多数情况下,运行时的大部分时间都是函数的执行。

      尝试和测量很简单,不是吗?谢谢,这正是我想要的。