5

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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Do you have any idea about @Andrew Orsich's index answer?
For javascript queries it's not possible to use indexes, because the javascript has to be evaluated for each object, which is selected with your query. So: if you have the choice between a built-in operator like $mod and a javascript, the javascript should always be slower, because the javascript is evaluated for each object and it can not use an index, while the builtin operator is evaluated only ones when your query is parsed by mongodb and it can use an index. It would probably be interesting, to benchmark this ...
Thanks for your answer. Just one more. What you mean with indexing can I read it from anywhere? I think I don't know what.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.