Dave
Dave

Reputation: 427

Sorting values in couchdb

I'm trying to pull scores back from this view

"scores": {
    "map": "function(doc) { emit(doc.appName, {_id:doc.username, username:doc.username, credits:doc.credits, avatar:doc.avatar}) }"
},

I pass in the appname and it returns the scores. The issue is the scores wont sort correctly. When I try to sort them they only sort by the first number so something like so

1500
50
7
900

As you can see the first numbers are sorted ASC but the whole number itself isn't. Is it possible to have couchdb sort the scores if the appname is the key?

Upvotes: 0

Views: 492

Answers (2)

Daniel
Daniel

Reputation: 8368

Use a complex key:

emit([doc.appName, doc.score], null)

Then query using a range:

startkey=["app1", 0]&endkey=["app1", {}]

Upvotes: 0

SingleNegationElimination
SingleNegationElimination

Reputation: 156138

is doc.appName a string? Turn it into a number:

function(doc) { 
    emit(parseInt(doc.appName), {_id:doc.username, username:doc.username, credits:doc.credits, avatar:doc.avatar});
    //   ^^^^^^^^
}

Upvotes: 1

Related Questions