简体   繁体   English

Mongodb JavaScript表达式和$ where

[英]Mongodb javascript expressions and $where

Are there any performance impacts using javascript expressions inside mongodb query instead standard BSON notation. 在mongodb查询中使用JavaScript表达式代替标准BSON表示法会对性能产生任何影响。 For example: 例如:

>db.myCollection.find( { a : { $gt: 3 } } );
>db.myCollection.find( { $where: "this.a > 3" } );

Will the first query be faster than the second one if there is no index on a column? 如果列上没有索引,第一个查询会比第二个查询快吗? Also, is there any way to write query 另外,有什么办法可以写查询

>db.myCollection.find( { $where: "this.a / 10 > 3" } );

or 要么

>db.myCollection.find( { $where: "this.a / this.b > 3" } );

without using $where notation? 不使用$ where表示法?

from here: 从这里:

http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-%7B%7B%24where%7D%7DClausesandFunctionsinQueries http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-%7B%7B%24where%7D%7DClausesandFunctionsinQueries

"Note: if a normal data-driven BSON query expression is possible, use that construction. Use $where only when you must it is significantly slower." “注意:如果可以使用普通的数据驱动的BSON查询表达式,请使用该构造。仅在必须显着降低速度的情况下才使用$ where。”

For your second question: can you do 关于第二个问题:你能做吗

{ a : { $gt: 0.3 } }

Will the first query be faster than the second one if there is no index on a column? 如果列上没有索引,第一个查询会比第二个查询快吗?

In both cases the first on will basically always be faster. 在这两种情况下,首次启动基本上总是会更快。 The $where clause will use the javascript engine. $where子句将使用javascript引擎。 There can only be one javascript engine running per instance, so it may have to wait. 每个实例只能运行一个JavaScript引擎,因此可能需要等待。 Additionally, the $where clause will have to move objects into and out of the javascript VM which adds overhead and will be slower. 此外, $where子句将必须将对象移入和移出javascript VM,这会增加开销,并且速度较慢。

Also, is there any way to write query... .find( { $where: "this.a / this.b > 3" } ); 另外,还有什么方法可以写查询... .find( { $where: "this.a / this.b > 3" } ); without using $where notation? 不使用$ where表示法?

The answer here is no . 答案是否定的 The query engine does not support any query where comparing data within an object. 查询引擎不支持在对象内比较数据的任何查询。 The only workarounds here are to: 唯一的解决方法是:

  1. Pre-compute the column ( which I'm sure you're trying to avoid ) 预计算列( 我确定您正在尝试避免该列)
  2. Run a for loop somewhere and do this "manually". 在某个地方运行for循环,然后“手动”执行。 This can be done with a Map/Reduce or a client-side query. 这可以通过Map / Reduce或客户端查询来完成。

Of course, both solutions are sub-optimal. 当然,两种解决方案都不是最优的。 This is definitely a whole in MongoDB's functionality. 这绝对是MongoDB功能的整体。

"Javascript executes more slowly than the native operators listed on this page, but is very flexible. See the server-side processing page for more information." “ Javascript的执行速度比本页上列出的本机运算符慢,但非常灵活。有关更多信息,请参见服务器端处理页面。”

See the following for more details: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D 有关更多详细信息,请参见以下内容: http : //www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D

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

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