You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Manolo Padron Martinez <ma...@gmail.com> on 2009/11/10 16:22:22 UTC

Recursive views?

hi:

I have some docs with N levels of hashes nesteds.. I wan't to get a
view that show the name of the fields and the fields that have into. I
mean something like this:

function(doc) {
  for (var i in doc)
    if (typeof doc[i] === "object")
      for (var j in doc[i])
        emit(i, j);
}

but for every level. There is any way to make something  like that?

Regards from Canary Islands

Manuel Padrón Martínez

Re: Recursive views?

Posted by Manolo Padron Martinez <ma...@gmail.com>.
Hi:

Thank you Norman. That was I was looking for. I tried something like
that yesterday, before send the mail and it fails (now I'm pretty sure
that was syntax error but yesterday I was overdue). The one that you
send me works perfectly.

Thanks again

Regards from Canary Islands

Manuel Padrón Martínez

Re: Recursive views?

Posted by Norman Barker <no...@gmail.com>.
On Tue, Nov 10, 2009 at 8:22 AM, Manolo Padron Martinez
<ma...@gmail.com> wrote:
> hi:
>
> I have some docs with N levels of hashes nesteds.. I wan't to get a
> view that show the name of the fields and the fields that have into. I
> mean something like this:
>
> function(doc) {
>  for (var i in doc)
>    if (typeof doc[i] === "object")
>      for (var j in doc[i])
>        emit(i, j);
> }
>
> but for every level. There is any way to make something  like that?
>
> Regards from Canary Islands
>
> Manuel Padrón Martínez
>

you can declare a function within a function in javascript and then
recurse that way since a view is a single outer function - I have done
it, it works ok. Just make sure that you emit less than than the input
document size or the view will get large.

e.g.

function(doc) {
      function walk(subdoc) {
        for (var p in subdoc)
        {
           if (typeof(p) === 'string')
             emit(p, null);

            if (typeof(subdoc[p]) === 'object')
               walk(subdoc[p]);
        }
     }

     walk(doc);
}

Norman