Find The Duplicate value in CouchDb

霸气de小男生 提交于 2019-12-12 09:57:24

问题


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);
  }
}

And I have the results like

["Bob"],["Peter"],["Bob"] ....

I'd Like to Have a reduce like

["Bob","Peter","Bob"] 

Or an Array with only duplicate

["Bob","Bob"] 

I don't understand How reduce works. foreach value reduce is called or only when the map is finished?


回答1:


Let's say you have the following three documents.

Bob:

{
  "_id": "89d9ffe10a33df504ecc8d7c9a975eed",
  "_rev": "1-dfc3128d8d80760f2cf40328dd24553e",
  "type": "user",
  "name": "Bob"
}

Peter:

{
  "_id": "89d9ffe10a33df504ecc8d7c9a98a0c6",
  "_rev": "1-820e231c44f9b3125db79e0c00bbc050",
  "type": "user",
  "name": "Peter"
}

Another Bob:

{
  "_id": "89d9ffe10a33df504ecc8d7c9a99f360",
  "_rev": "1-dfc3128d8d80760f2cf40328dd24553e",
  "type": "user",
  "name": "Bob"
}

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);
  }
}

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
    }
  ]
}

As these results are collated (sorted) by name, duplicate name values will be adjacent to each other in the results.

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
    }
  ]
}

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
    }
  ]
}


来源:https://stackoverflow.com/questions/33812501/find-the-duplicate-value-in-couchdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!