couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Doug Reeder <>
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 < 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 ( < 0) {
    var key = [];
    var i;
    for (i=0; i<doc._id.length; ++i)

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 '\[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

View raw message