couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steven.Prent...@nextgen.net
Subject How do I SUM(values in a field array) using Views
Date Wed, 02 Jun 2010 05:38:00 GMT
Hi say I had product documents that looked like this (formatted for 
readability):

{
"_id":"p9",
"_rev":"2-9a109fb7d9f116441876e0d6401ec1ba",
"product_name":"Apple iPhone 3Gs 32GB",
"Stores":[
                {
                 "name":"Store A",
                 "item_count":558
                },
                {
                 "name":"CompuShop",
                 "item_count":4878
                },
                {
                 "name":"Kerry's Computer Shop",
                 "item_count":54474
                }
            ]
}

and a view function:
function(doc)
{
  for (id in doc.Stores)
  {
    if(doc.product_name && doc.Stores[id].item_count)
    { 
      emit(doc.product_name, doc.Stores[id].item_count);
    }
  } 
}

and a reduce function:
function(keys, values) {
   return sum(values);
}

Why, when I look at the resulting view in Futon does it look correct but 
when I view in browser it gives me a null key and a total of all items as 
opposed to one for each product? Or if I specify a product using ?key= , 
why do i still get key:null 

.../_design/inventory/_view/total_items
{"rows":[
{"key":null,"value":8452613}
]}


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