I don't know the way i can design the next condition in CouchDB.

I've got a logger web application that monitors the number of products have been in a warehouse. To simplify the issue we simply need to be aware of final amount products presently in warehouse and just how lengthy is the items stays in warehouse before it ships. Allows the warehouse have only footwear but each shoe have different id and want to help keep track by id.

MySQL schema looks like this

    id       name        date-in       data-out
    1        shoe       08/0/2010      null
    2        shoe       07/20/2010     08/01/2010

The output will be
    Number of shoe in warehouse:  1 
    Average time in warehouse:    14 days


jhs' response is great, however i just added something:

To make use of the build-in reduce function for that avg calculation (_stats inside your situation), make use of two "separate" sights. But when your map-function is the same, CouchDB will identify might not generate another index for your second view. By doing this you could have one map function feeding multiple reduce functions.

If each shoe is really a document, having a date_in and date_out, your reduce function will +1 when the date_out is null, and + (no change) if date_out isn't null. That provides you with the entire count of footwear within the warehouse.

To compute the typical time, for every shoe, you realize time within the warehouse. Therefore the reduce function simply builds up the typical. Since reduce functions should be commutative and associative, you utilize another average formula. The simplest strategy is to lessen to some [sum, count] array, where sum is definitely an accumulator ever for those footwear, and count is really a counter for the amount of footwear counted. Then your client simply divides sum / count to compute the ultimate average.

I believe you can mix these two into one large reduce if you would like, possibly accumulating a {"shoes in warehouse": 1, "average time in warehouse": [253, 15]} type of object.

However, if you're able to accept two different sights with this data, then there's a shortcut for that average. Within the map, emit(null, time) where time it's time put in the warehouse. Within the reduce, set the whole reduce value to _stats (see Built-in reduce functions). The vista output is going to be an item using the sum and count already calculated.