couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Doug Reeder <reeder...@gmail.com>
Subject Subselect Possible?
Date Sun, 26 Sep 2010 23:46:54 GMT
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
reeder.29@gmail.com
http://reeder29.livejournal.com/
https://twitter.com/reeder29

https://twitter.com/hominidsoftware
http://outlinetracker.com









Mime
View raw message