Are there any performance impacts using javascript expressions inside mongodb query instead standard BSON notation. 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?
In both cases the first on will basically always be faster. The
$whereclause will use the javascript engine. There can only be one javascript engine running per instance, so it may have to wait. Additionally, the$whereclause will have to move objects into and out of the javascript VM which adds overhead and will be slower.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:
forloop somewhere and do this “manually”. This can be done with a Map/Reduce or a client-side query.Of course, both solutions are sub-optimal. This is definitely a whole in MongoDB’s functionality.