在CouchDb中查找重复值 [英] Find The Duplicate value in CouchDb
问题描述
我想在CouchDB中找到相同的值。我的地图是
I want to find same value in CouchDB. My Map is
function(doc) {
var user = [];
if(doc.type = 'user'){
user.push(doc.name);
emit( doc.type, user);
}
}
我得到的结果是
["Bob"],["Peter"],["Bob"] ....
我想减少点
["Bob","Peter","Bob"]
或仅包含重复项的数组
["Bob","Bob"]
我不了解reduce如何工作。
foreach value reduce是调用还是仅在映射完成时调用?
I don't understand How reduce works. foreach value reduce is called or only when the map is finished?
推荐答案
假设您有以下三个文档。
Let's say you have the following three documents.
鲍勃:
{
"_id": "89d9ffe10a33df504ecc8d7c9a975eed",
"_rev": "1-dfc3128d8d80760f2cf40328dd24553e",
"type": "user",
"name": "Bob"
}
Peter:
{
"_id": "89d9ffe10a33df504ecc8d7c9a98a0c6",
"_rev": "1-820e231c44f9b3125db79e0c00bbc050",
"type": "user",
"name": "Peter"
}
另一个鲍勃:
{
"_id": "89d9ffe10a33df504ecc8d7c9a99f360",
"_rev": "1-dfc3128d8d80760f2cf40328dd24553e",
"type": "user",
"name": "Bob"
}
您要查找具有重复的名称
值的文档。只需使用以下地图功能在 name
字段上创建视图:
You want to find documents with duplicate name
values. Simply create a view on the name
field with the following map function:
function (doc) {
if (doc.type == "user") {
emit(doc.name);
}
}
使用内置的 _count
缩小功能。如果使用 reduce = false
查询此视图,则会得到以下结果:
Use the built in _count
reduce function. If you query this view with reduce=false
, you will get the following results:
{
total_rows: 3,
offset: 0,
rows: [
{
id: "89d9ffe10a33df504ecc8d7c9a975eed",
key: "Bob",
value: null
},
{
id: "89d9ffe10a33df504ecc8d7c9a99f360",
key: "Bob",
value: null
},
{
id: "89d9ffe10a33df504ecc8d7c9a98a0c6",
key: "Peter",
value: null
}
]
}
由于这些结果是按名称$ c排序(排序)的$ c>,重复的
name
值将在结果中彼此相邻。
As these results are collated (sorted) by name
, duplicate name
values will be adjacent to each other in the results.
或者,您可以查询使用 group = true
的视图,您将得到以下结果:
Alternatively, you can query this view with group=true
and you will get the following results:
{
rows: [
{
key: "Bob",
value: 2
},
{
key: "Peter",
value: 1
}
]
}
任何值
为 2
或更高的结果表示重复。确定了 Bob
Bob 使用 reduce = false& = key Bob
,这将为您提供以下结果:
Any result with a value
of 2
or greater indicates that there are duplicates. Having identified that there are duplicate entries for the key of "Bob"
, you can then query for all of the "Bob"
s using reduce=false&key="Bob"
, which will give you the following results:
{
total_rows: 3,
offset: 1,
rows: [
{
id: "89d9ffe10a33df504ecc8d7c9a975eed",
key: "Bob",
value: null
},
{
id: "89d9ffe10a33df504ecc8d7c9a99f360",
key: "Bob",
value: null
}
]
}
这篇关于在CouchDb中查找重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!