Sorry, my earlier response seems to have gone astray.
Thank you, but this map and reduce function produce exactly the same results as
mine.
Is there a general method of structuring data and map-reduce functions to do
the same thing that a subselect does?
On Sep 26, 2010, at 8:16 PM, Paul Davis wrote:
> Hrm, I haven't thought this through to the end, but something that
> might get you started:
>
> Change your emit to emit(key, 1);
>
> Change your reduce to:
>
> function(keys, vals) {
> return sum(vals);
> }
>
> Then query like such:
>
> ?key=query_key&limit=1
> ?group_level=length(query_key)&key=query_key
>
> It seems like there should be a fancy way to get the reduce call to
> tell you if the original node had < 0 work but I'm distracted by other
> code right now.
>
> HTH,
> Paul Davis
>
> On Sun, Sep 26, 2010 at 7:46 PM, Doug Reeder <[email protected]> wrote:
>> I've searched the wiki and googled for "subselect" and "subquery", and not
>> found anything relevelant.
>>
>> I'm evaluating whether CouchDB can handle an app which currently stores a
>> tree structure in a relational database using the "materialized path"
>> represenation. For example, the item with path "QG" is a child of the item
>> with path "Q" and a parent of the item with path "QGC".
>>
>> The trickiest query is, in English, "find all items with work less than zero
>> and whether they have any children with work less than zero". The SQL for
>> this is
>> SELECT item.*, EXISTS (SELECT work FROM item AS d WHERE d.path > item.path
>> AND d.path <= item.path || '\ufffd' AND d.work < 0) AS undonePrereq, FROM
>> item WHERE work < 0
>>
>> Can this be done in CouchDB, using a limited, small number of queries?
>>
>> My closest sally is the map function
>> function(doc) {
>> if (doc.work < 0) {
>> var key = [];
>> var i;
>> for (i=0; i<doc._id.length; ++i)
>> key.push(doc._id.charCodeAt(i));
>> emit(key, doc.work);
>> }
>> }
>>
>> and the reduce function
>> function(keys, values, rereduce) {
>> var shortestKeyLength = Infinity;
>> for (var i=0; i<keys.length; ++i) {
>> if (keys[i][0].length < shortestKeyLength)
>> shortestKeyLength = keys[i][0].length;
>> }
>>
>> return [keys.length, shortestKeyLength,
>> keys[0][0].slice(0,shortestKeyLength), values.length];
>> }
>>
>> A query such as
>> curl -X GET
>> 'http://127.0.0.1:5984/tracker1/_design/trackerA/_view/undoneArrayNum?startkey=\[45,65,65\]&endkey=\[45,65,9999\]&group_level=2'
>> will return the data necessary for items at the second level, but the
>> desired items could be at any level.
Doug Reeder
[email protected]
http://reeder29.livejournal.com/
https://twitter.com/reeder29
https://twitter.com/hominidsoftware
http://outlinetracker.com