incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nitin Borwankar <ni...@borwankar.com>
Subject Re: how to access key names of a document?
Date Sun, 03 May 2009 00:57:03 GMT
Anand,

Thanks much.
I did muddle around and figure that out in the meanwhile by playing around
in futon.
But when I create the design doc and put this code as

{

[...]

views: {

  cnt : {
     map:  function () {....},
     reduce: function() {...}
  }

}

37% of all statistics are made up on the spot
-------------------------------------------------------------------------------------
Nitin Borwankar
nborwankar@gmail.com


On Fri, May 1, 2009 at 10:07 PM, Anand Chitipothu <anandology@gmail.com>wrote:

> > I have a database with documents that have varying number of fields.
> > I need to create a view that displays
> >
> > { field_name1 : count, ..... }
> >
> > i.e. a report that displays how many times a field occurs in the db.
> > It's like a word frequency vector in Lucene except for the fact that
> these
> > are field names.
> >
> > So I am missing something basic but I need to loop over all keys of a doc
> > without knowing their names in advance.
> > so I do something like
> >
> > var x = {}
> > for ( var k in doc.keys ) {
> >  x[k] = 1
> > }
> > emit ( x, 1 ) <---- is this the right thing to emit ?
>
> No, you should do this:
>
> function(doc) {
>    for (var k in doc)
>        emit(k, 1);
> }
>
> And your reduce function should be:
>
> function(keys, values, rereduce) {
>    return sum(values);
> }
>
> I'm assuming that you want global frequency of k1, k2 etc in documents
> like {"k1": "v1", "k2", "v2", ..}
>
> If name of your design document is d and name of your view is v, you
> can get the frequency from:
>
> http://localhost:5984/dbname/_design/d/_view/v?reduce=true
>
> > newbie questions
> > a) whats the syntax to access all keys of a doc
>
> You can achieve this by writing a view with the following map function:
>
> function(doc) {
>    var keys = [];
>    for (key in doc)
>        keys.push(key);
>    emit(doc.id, keys);
> }
>
> > b) what's the right thing to emit
> > c) what do I do in the reduce phase ?
>
> Answered above.
>
> -Anand
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message