CouchDB 视图:删除重复项*和*按时间排序 [英] CouchDB Views: remove duplicates *and* order by time

查看:45
本文介绍了CouchDB 视图:删除重复项*和*按时间排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的上一个问题的一个很好的回答,我已经部分解决了我在使用 CouchDB 时遇到的问题.

Based on a great answer to my previous question, I've partially solved a problem I'm having with CouchDB.

这导致 新视图.

现在,我需要做的下一件事是从该视图中删除重复项同时按日期排序.

Now, the next thing I need to do is remove duplicates from this view while ordering by date.

例如,我可以这样查询该视图:

For example, here is how I might query that view:

GET http://scoates-test.couchone.com/follow/_design/asset/_view/by_userid_following?endkey=[%22c988a29740241c7d20fc7974be05ec54%22]&startkey=[%22c988a29740241c7d20fc7974be05ec54%22,{}]&descending=true&limit=3

导致:

HTTP 200 http://scoates-test.couchone.com/follow/_design/asset/_view/by_userid_following
http://scoates-test.couchone.com > $_.json.rows
[ { id: 'c988a29740241c7d20fc7974be067295'
  , key: 
     [ 'c988a29740241c7d20fc7974be05ec54'
     , '2010-11-26T17:00:00.000Z'
     , 'clementine'
     ]
  , value: 
     { _id: 'c988a29740241c7d20fc7974be062ee8'
     , owner: 'c988a29740241c7d20fc7974be05f67d'
     }
  }
, { id: 'c988a29740241c7d20fc7974be068278'
  , key: 
 [ 'c988a29740241c7d20fc7974be05ec54'
     , '2010-11-26T15:00:00.000Z'
     , 'durian'
     ]
  , value: 
     { _id: 'c988a29740241c7d20fc7974be065115'
     , owner: 'c988a29740241c7d20fc7974be060bb4'
     }
  }
, { id: 'c988a29740241c7d20fc7974be068026'
  , key: 
     [ 'c988a29740241c7d20fc7974be05ec54'
     , '2010-11-26T14:00:00.000Z'
     , 'clementine'
     ]
  , value: 
     { _id: 'c988a29740241c7d20fc7974be063b6d'
     , owner: 'c988a29740241c7d20fc7974be05ff71'
     }
  }
]

如您所见,clementine"出现了两次.

As you can see, "clementine" shows up twice.

如果我更改视图以将水果/资产名称作为第二个键(而不是时间)发出,我可以更改分组深度以折叠这些,但这并不能解决我的按时间排序要求.同样,通过上述设置,我可以按时间排序,但我不能将重复的资产名称折叠成单行(例如,每页允许 10 个资产).

If I change the view to emit the fruit/asset name as the second key (instead of the time), I can change the grouping depth to collapse these, but that doesn't solve my order-by-time requirement. Similarly, with the above setup, I can order by time, but I can't collapse duplicate asset names into single rows (to allow e.g. 10 assets per page).

很遗憾,这不是一个简单的问题.也许这个聊天记录会有所帮助.

Unfortunately, this is not a simple question to explain. Maybe this chat transcript will help a little.

请帮忙.恐怕我需要做的还是做不到.

Please help. I'm afraid that what I need to do is still not possible.

S

推荐答案

你可以使用列表函数来做到这一点.这是一个生成一个非常简单的列表的示例,该列表包含所有所有者字段,没有欺骗.您可以轻松修改它以生成 json 或 xml 或任何您想要的.

You can do this using list function. Here is an example to generate a really simple list containing all the owner fields without dupes. You can easily modify it to produce json or xml or anything you want.

将其放入您的资产设计文档中,并在 lists.nodupes 中使用,如下所示:http://admin:123@127.0.0.1:5984/follow/_design/assets/_list/nodupes/by_userid_following_reduce?group=true

Put it into your assets design doc inside the lists.nodupes and use like this: http://admin:123@127.0.0.1:5984/follow/_design/assets/_list/nodupes/by_userid_following_reduce?group=true

function(head, req) {
    start({
          "headers": {
          "Content-Type": "text/html"
          }
         });
    var row;
    var dupes = [];
    while(row = getRow()) {
    if (dupes.indexOf(row.key[2]) == -1) {
        dupes.push(row.key[2]);
        send(row.value[0].owner+"<br>");
    }
    } 
}

这篇关于CouchDB 视图:删除重复项*和*按时间排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆