couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Norman Barker <norman.bar...@gmail.com>
Subject Re: Recursive views?
Date Tue, 10 Nov 2009 15:31:13 GMT
On Tue, Nov 10, 2009 at 8:22 AM, Manolo Padron Martinez
<manolopm@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

Mime
View raw message