开发者

Map/Reduce in CouchDB with multiple parameters?

I am wondering how to use CouchDB's map/reduce with multiple parameters. For exampl开发者_如何转开发e, if I have teams that have players with ages and genders, I assume I would do this for my map function:

"function(doc){
  if(doc.team_name) {
    emit(doc.team_name, doc);
  }
}"

However, I am unsure how to write a reduce function to get the oldest male player on the team or the youngest female or whatever arbitrary query. Can I pass in parameters in the URL or do I have to write multiple views?

Thanks in advance,

Ben


Reduce function has a bit different purpose. Reduce function is grouping some value for all the documents it processes. So you can e.g. sum players salaries, or age, or count them.

If you want to get the oldest player in the team, just set your key in emit function to [team, age]. View is always sorted by key.

function(doc) {
  if (doc.team_name) {
    emit([doc.team_name, doc.age], doc);
  }
}

now just query your view. Add parameter descending=true, so the oldest player is the first. By default view order is ascending. If you want to get players in specific team (still sorted by age) add parameters: startkey=[<team>, 999]&endkey=[<team>,0]&descending=true

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜