couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Hardtke <da...@cloudant.com>
Subject Re: Writing View to Get Unique Values in Arrays Across Documents
Date Wed, 19 Jan 2011 23:48:43 GMT

The format won't be exact, but you could use an array for a key:

map:
function(doc) {
     for (var i = doc.arrayData) {
         emit([doc.idToRetrieveBy, doc.arrayData[i]], null)
     }
}
reduce:
_count  (i.e. the builtin)

you can then run the following query for idToRetrieveBy=1

GET 
/dbname/_design/<design_doc>/_view/<view_name>?group_level=2&startkey=[1]&endkey=[1,{}]

You'll get a result like:

{rows:[
{"key":[1,"Homer"],"value":2},
{"key":[1,"Marge"],"value":2},
...
]
}


On 01/19/11 15:22, Matthew Woodward wrote:
> Been banging my head on this one only briefly but figured I'd ask before
> banging further.
>
> I have documents with a property that's an array. I want to write a view to
> retrieve all documents based on an ID in the document (not the document ID,
> but another ID that will exist in multiple documents), and the ultimate end
> result I want is the distinct/unique values from the array data across these
> documents.
>
> To give a concrete example, let's say I have the following two documents:
>
> DOCUMENT 1:
> idToRetrieveBy: 1
> arrayData: ['Homer','Marge','Bart']
>
> DOCUMENT 2:
> idToRetrieveBy: 1
> arrayData: ['Homer','Marge','Bart','Lisa','Maggie']
>
>  From the view, what I'd want to get back is:
> ['Homer','Marge','Bart','Lisa','Maggie']
>
> Any ideas on how to go about that, or is that something I'd have to crunch
> on the application side of things?
>
> Thanks.
>


Mime
View raw message