如何从map函数本身过滤couchdb中的重复文档

如何从map函数本身过滤couchdb中的重复文档,couchdb,Couchdb,形势 我有一组名为“接触”的文档,指向多对一关系中一组名为“消费者”的文档 _id : 0 type : touch consumer: 1 //three touches pointing to one consumer agent : bill _id : 1 type : touch consumer: 1 //three touches pointing to one consumer agent : ted _id : 2 type

形势
我有一组名为“接触”的文档,指向多对一关系中一组名为“消费者”的文档

_id     : 0
type    : touch
consumer: 1 //three touches pointing to one consumer
agent   : bill

_id     : 1
type    : touch
consumer: 1 //three touches pointing to one consumer
agent   : ted

_id     : 2
type    : touch
consumer: 1 //three touches pointing to one consumer
agent   : ted
实际上,我只是试图加载ted“接触过”的消费者的信息,所以我的查询如下所示

 get_touches?agent=ted&include_docs=true
问题
问题是我不想多次显示相同的消费者信息。受这个答案的启发,我编写了下面的map函数来过滤结果

"map": "var ids = []; 
        function(doc){
            if(doc.type === \"touch\" && !ids.contains(doc.consumer)){         
               ids.push(doc.consumer); 
               emit(doc._id, {\"_id\": doc.consumer});
            }
        }"
它只返回一个空集。我不确定我做错了什么,甚至不确定这是否真的有效

更多信息

许多代理可以多次触摸单个消费者,因此将触摸代理或触摸存储在消费者文档中不是一个好的答案。

a好的,在地图功能中,您不能同时访问多个文档。将数据推入map函数之外的数组的示例不会跨文档存储数据

这将是一个简单的reduce函数,尽管对于任何给定的键只需要第一个文档

另一种方法是按用户存储数据,即使用用户密钥更新文档,然后添加到存储所有触摸的列表中。显然,如果您想通过代理访问数据,这是有缺点的