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

查看:76
本文介绍了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'
     }
  }
]

如您所见,克莱门汀"出现了两次.

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.

将其放入list.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天全站免登陆