Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up the random number example #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions content/patterns/random-attribute.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,16 @@ your number against the stored attribute.

<% code 'javascript' do %>
> rand = Math.random()
> cmp = Math.random()
> result = db.docs.findOne( { key : 2, random : { $gte : rand } } )
> if ( result == null ) {
> result = db.docs.findOne( { key : 2, random : { $lte : rand } } )
> result = db.docs.findOne( { key : 2, random : { $lt : rand } } )
> }
<% end %>


Note that we're not going for equality alone because the chances of
that to occur are low. So we try either '$gte' or '$lte' with equal
probability but knowing that in some cases it may not return a result,
even though there are documents in the result. For that reason, an
empty result must be verified by doing a search in the opposite
that to occur are low. So we try '$gte', and as there is a possibility
of it may not return a result, we must do a search in the opposite
direction.

The final -- but important -- detail about this query is that both the
Expand Down