CouchDB按链接的doc属性排序

时间:2015-05-06 08:53:20

标签: javascript mapreduce couchdb

我有以下数据:

payment1: {
    id: "payment1",
    categoryId: "category1"
}

payment2: {
    id: "payment2",
    categoryId: "category2"
}

payment3: {
    id: "payment3",
    categoryId: "category2"
}

category1: {
    id: "category1",
    name: "Food"
}

category2: {
    id: "category2",
    name: "Leisure"
}

我需要的是按类别NAME对付款数据进行排序的视图。是否可以在CouchDB中使用?

提前致谢! 伊万

1 个答案:

答案 0 :(得分:1)

可能与您想要的有点不同。

这是地图:

function (o) {
  if (o.categoryId) {
    emit(o.categoryId);
  } else {
    emit(o.id);
  }
}

您可以使用/mydb/_design/mydesign/_view/myview?include_docs=true调用它并获取:

{"rows":[
   {"key":"category1", "doc":{"id":"payment1","categoryId":"category1"},
   {"key":"category1", "doc":{"id":"category1","name":"Food"},
   {"key":"category2", "doc":{"id":"payment2","categoryId":"category2"},
   {"key":"category2", "doc":{"id":"payment3","categoryId":"category2"},
   {"key":"category2", "doc":{"id":"category2","name":"Leisure"}
]}

换句话说,付款按类别分组,它们也与类别名称一起加入。但是,它们按类别ID排序,但不按类别名称排序。

您想要的是两种不同的排序(即“链式地图减少”)。