Wiz
Wiz

Reputation: 44

CouchDB view distinct/unique value from two fields

"myview": {
           "map": "function(doc) {if(doc.type == 'call') {emit([doc.from_user, doc.to_user], doc.type);} }",
           "reduce": "function (key, values) {return values;}"
       }

POST request with group = true

{
"keys":[["123456"], ["123456"]]
}

How can I get unique doc based on value exists in either in from_user or to_user?

Upvotes: 0

Views: 764

Answers (1)

Ingo Radatz
Ingo Radatz

Reputation: 1225

Emit the values of doc.from_user and doc.to_user as single keys.

e.g.

emit(doc.from_user, doc.type);

emit(doc.to_user, doc.type);

Every row of the view result includes the doc._id and you can also get the doc in the view result by using the query param include_docs=true.

Finally you request your view with the query param ?key="your_value" and you will get every row with that value as key.


If you want to know whether the value is from doc.from_user or doc.to_user just emit that information as part of the value or build as multi key like

emit([doc.from_user, 'from_user'], doc.type);

emit([doc.to_user, 'to_user'], doc.type);

Then you can request

?startkey=["your_value","from_user"]&endkey=["your_value","to_user"]

Upvotes: 1

Related Questions