Count number of keys by view


#1

Dear bros,
I use couchbase 2.5.0
My bucket has 20M keys (I see on console)

I use view to:

  • get biggest key
  • count number of keys
  • total key size
  • average key size
    But when I run view, it has only 20039 keys.
    Pls help me to get the result i want.

This is my code:

    /*map function*/
  function (doc, meta) {
    if (meta.type == "json") {
      var size = JSON.stringify(doc).length;
      emit(meta.id, size);
    }
    if (meta.type == "base64") {
      var size = decodeBase64(doc).length;
      emit(meta.id, size);    
    }
  }


/*reduce function*/

function (keys, values, rereduce) {  
  if(!rereduce) {   
    var total = 0,
      count = 0,
      max_key = '',
      max_key_size = 0;
    for (v in values) {
      total+= values[v];     
      count++;
      if(values[v] > max_key_size){
        max_key_size = values[v];
        max_key = keys[v];
      }
    }
  } else {
    var total = 0,
      count = 0,
      max_key = '',
      max_key_size = 0;
    for (v in values) {
      total += values[v]['total'];
      count += values[v]['count'];
      if(values[v]['max_key_size'] > max_key_size){
        max_key_size = values[v]['max_key_size'];
        max_key = values[v]['max_key'];
      }
    }    
  }
  var average = total / count;
  return {max_key: max_key,
    max_key_size:max_key_size,
    count: count,
    total: total,
    average: average
  };
}

Thanks and best regards,


#2

It sounds like you’re likely using the “dev” view, instead of the “production” view. Once published, the view will return results from the full dataset in the bucket. You may want to read some of the documentation about development and production views.