Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb Fast Queries Javascript

I am reading that document: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24mod

$mod

The $mod operator allows you to do fast modulo queries to replace a common case for where clauses. For example, the following $where query:

db.things.find( "this.a % 10 == 1")

can be replaced by:

db.things.find( { a : { $mod : [ 10 , 1 ] } } )

So I didn't understand what fast means here. Performance?

like image 903
kamaci Avatar asked Oct 10 '22 18:10

kamaci


2 Answers

I have not benchmarked this, but it will probably indeed mean performance. Apparently the "$where" executes javascript for each object, but "$mod" is a mongodb native operator, which should be much faster, because there is no need to execute any javascript for each object. Have also a look at the following sentence from the documentation:

Javascript executes more slowly than the native operators listed on this page, 
but is very flexible.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D

like image 163
aurora Avatar answered Oct 13 '22 10:10

aurora


Any javascript/regex queries in mongodb can't use indexes and work slow. So answer on your question is yes, documentation say about performance.

More info about server side javascript you can find here

like image 29
Andrew Orsich Avatar answered Oct 13 '22 12:10

Andrew Orsich