有没有办法从结果中清除重复记录? [英] is there a way to clear duplication record from results?
问题描述
我有一个视图,但是确实从视图结果中复制了文档,如下所示,如何获得重复的结果并获得唯一的?预先谢谢你
I have a view but did have duplicated documents from the results of view as like following, how can I get the duplicate results and get the unique? thank you in advance
{
"total_rows": 9,
"offset": 0,
"rows": [
{
"id": "xxxx",
"key": "12345",
"value": {
"_id": "abc123",
"_rev": "4-8db4da81d1e20afcea0a328fb16e7ec8",
"field1": "abc",
"field2": "dfr"
},
{
"id": "xxxx",
"key": "12345",
"value": {
"_id": "abc123",
"_rev": "4-8db4da81d1e20afcea0a328fb16e7ec8",
"field1": "abc",
"field2": "dfr"
},
]
}
视图是这样的
function(doc) {
if(doc){
for (var i in doc.item){
emit(doc.item[i].key,doc);
}
}
}
视图由调用 ...._ view/duplicate?key ="12345"
view called by ...._view/duplicate?key="12345"
执行以下reduce时,我总是遇到此错误:" error":"reduce_overflow_error","reason":"Reduce输出必须缩小更多 迅速:
I always had this error when execute the following reduce :"error":"reduce_overflow_error","reason":"Reduce output must shrink more rapidly:
function (keys, values, rereduce) {
var uniqueKey = [];
var newValues = [];
for (var i=0; i<values.length; i++) {
if (uniqueKey.indexOf(values[i]._id)==-1) {
uniqueKey.push(values[i]._id);
newValues.push(values[i]);
}
}
return newValues;
}
推荐答案
问题是您的地图功能:
function(doc) {
if(doc){
for (var i in doc.item){
emit(doc.item[i].key,doc);
}
}
}
这样做,您会多次发出相同的文档,因此,如果视图结果中有重复的文档,也就不足为奇了.如果要返回文档的所有项目而没有重复项,则可以执行以下操作:
By doing that, you emit the same document several times, therefore it shouldn't come as a surprise that you have duplicate documents in your view's results. If you want to return all the items for a document without any duplicate, you can do something as simple as that :
function(doc) {
if(doc.item) {
emit(doc.item, null)
}
}
还请注意,我执行emit(doc.item, null)
而不是emit(doc.item, doc)
.发出文档是一种不好的做法,您应该改为使用选项include_docs=true
查询视图.否则,您的视图索引将具有与数据库相同的大小.
Also please note that I do emit(doc.item, null)
and not emit(doc.item, doc)
. It is a bad practice to emit the document, you should instead query the view with option include_docs=true
. Otherwise your view index will have the same size as your database.
这篇关于有没有办法从结果中清除重复记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!