简体   繁体   English

CouchDB按链接的文档属性排序

[英]CouchDB sorting by linked doc property

I have the following data: 我有以下数据:

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"
}

What I need is a view for sorting the payment data by category NAME. 我需要的是用于按“ NAME”类别对付款数据进行排序的视图。 Is it possible in CouchDB? 在CouchDB中可以吗?

Thanks in advance! 提前致谢! Ivan 伊万

What is possible is a bit different from what you want. 可能与您想要的有点不同。

Here is the map: 这是地图:

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

You'll call it with /mydb/_design/mydesign/_view/myview?include_docs=true and get: 您可以使用/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"}
]}

In other words, payments are grouped by category, they are also joined with category's name. 换句话说,付款是按类别分组的,它们也与类别名称结合在一起。 However, they are sorted according category ID but not category's name. 但是,它们是根据类别ID而不是类别名称进行排序的。

What you want would require two different sorts (ie "chained map reduce"). 您想要的内容将需要两种不同的类型(即“链接映射减少”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM