ChallengeQuerying relative information with RavenDB

time to read 2 min | 266 words

Originally posted at 11/30/2010

Let us say that we have the following document:

  "Name": "Ayende",
  "LastScore": 239.2,
  "MaxScore": 392.6

And we want to find all documents whose last score is within 10% from their max score.

If I was using SQL, I could express the query like this:

select * from GameResults
where (LastScore + MaxScore/100 )> MaxScore

How would you create such a query using RavenDB?

  • RavenDB has the notion of linear queries, which would allow executing the above query, but I don’t want to use that.
  • RavenDB’ indexes don’t support any way to compare two fields to one another.

There is a solution, though, and it is quite an elegant one.

More posts in "Challenge" series:

  1. (03 Jan 2020) Spot the bug in the stream–answer
  2. (15 Feb 2010) Where is the optimization?