You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Duc Phan <th...@gmail.com> on 2010/11/17 22:34:52 UTC
Merge Documents
Hello again,
Last week I posted a problem about getting documents merged on the same
field.
Few people made some suggestions and ideas such as combine two documents
with same field into one.
Well I run into a situation where I can't combine the documents.
Scenario:
I have documents of two types:
-------------------------
Type: Number
Number : (1-1000)
--------------------------
Type: Person
Name : Some Name
Number : (random number from 1 - 1000)
Its simple to find all the numbers that are used by creating a view of all
the Number with Type = Person.
But, how do I find all the numbers that aren't being used yet?
I can pull up the view of all the numbers and compare those with the used
numbers on client side, but I prefer not to.
Is there a way to create a view for this problem?
Thank you in advance for any help or suggestions.
BTW. I tried the Collation method and I still don't know how to get the view
I want.
Re: Merge Documents
Posted by Simon Metson <si...@googlemail.com>.
Hi,
> Its simple to find all the numbers that are used by creating a view
> of all
> the Number with Type = Person.
> But, how do I find all the numbers that aren't being used yet?
> I can pull up the view of all the numbers and compare those with the
> used
> numbers on client side, but I prefer not to.
How about a view like:
function(doc) {
if (doc.type == 'Number') {
emit(doc.Number, 1);
}
if (doc.type == 'Person') {
emit(doc.Number, -1);
}
}
with a _sum reduce, query that view with ?group=true then client side
take out values that are greater than 0 using array.filter:
js> function unused(element, index, array){
return element.value > 0;
}
js> data.rows.filter(unused)
[object Object],[object Object],[object Object],[object Object],
[object Object]...
The size of the array you have to filter will always be the number of
Number documents, so you can have lots of People in the database but
have a known size of json to filter client side.
I can't think of a way to do it with a view slice, possibly because
I've not had my morning coffee yet...
Cheers
Simon