On Thu, May 28, 2009 at 9:26 PM, Boris Schmidt <[email protected]> wrote:
> Hi!
>
> I'm quite new to CouchDB and map/reduce and played a little around with it.
> Currently, I am trying to find pairs of two documents which fulfill a special
> condition. E.g., consider the following documents in the store (_id and _rev
> are left out): [{price: 1},{price: 5},{price: 9},{price: 1000}]
>
> Now I would like to find all pair of documents, where (doc1.price + 4 ==
> doc2.price). How can I express this in couchdb? I expect the following
> result: [[{price: 1},{price: 5}], [{price: 5},{price:9}]]
>
> My first try was:
> map: function(doc){
> emit(null, doc);
> }
> reduce: function(key, values){
> for(var i in values){
> for(var j in values){
> if(values[i].price + 4 == values[j]) return [values[i], values[j]];
> }
> }
> }
>
> But then I recognized the rereduce property in CouchDB and that the values of
> the reduce funtion can be only intermediate results. So how can I express
> what I want to do in map reduce?
>
> Thx!
> Boris
> --
> Nur bis 31.05.: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate und
> Telefonanschluss nur 17,95 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02
>
I just recognized that my reduce funtion cannot work as expected, I
have to collect the combinations and return them afterwards:
map: function(doc){
emit(null, doc);
}
reduce: function(key, values){
var combs = [];
for(var i in values){
for(var j in values){
if(values[i].price + 4 == values[j]) combs.push([values[i], values[j]]);
}
}
return combs;
}
This works for cases where no rereduce takes place, but in other cases
it still have problems. How do I have to design my map/reduce
functions so that they cover my use case?
Thx!
Boris