couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tom van Neerijnen <>
Subject Sorting a view by a reduce result (or a better document structure or M/R)
Date Fri, 11 Oct 2013 15:09:58 GMT
Hi all

I'm developing a ranking service I'd like to throw scores for multiple
users at and later be able to retrieve a list of users ranked/ordered by
their scores. Problem is if I make it time series data and accumulate the
total score in a reduce I can't put the score in the key to sort by.

Does anyone have suggestions as to how I could re-jig my documents and/or
M/R to accommodate this kind of output or should I simply consider storing
a total document along side all my time series documents? My concern with
storing a total document is I can never be sure the total in the document
exactly matches my M/R over my time series data because I'd have to have an
app query the view and, using the result of the view query, update the
total document, so that's a last resort.

I considered sorting in a list function but we're potentially looking at
millions of users so that's not really an option.

I'm imagining time series documents like so:
"abcdefghi" : {
  "userId": "jim",
  "scoreChange": 10,
  "country": "GB",
  "game": "solitaire",
  "date": "2013-10-08T13:50:19"

"jklmnopqr" : {
  "userId": "jim",
  "scoreChange": -5,
  "country": "GB",
  "game": "solitaire",
  "date": "2013-10-08T13:51:19"

"jklmnopqr" : {
  "userId": "james",
  "scoreChange": 4,
  "country": "GB",
  "game": "freecell",
  "date": "2013-10-08T13:52:19"

I could then have this M/R for, as an example, ranks by game then user:
"by_game": {
    "map": "function(doc) { emit([, doc.userId], doc.scoreChange)}",
    "reduce": "function(keys, values, rereduce) { return sum(values); }"

and get this list from it:


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message