niedziela, 21 lutego 2016

couchDb count

The best way to learn is to take and learn by approaching a real problem..
Yesterday I learned that my thought-containg app is just microblogging app so no more thinking about thinking app.

In the aforementioned app I was tightly tied to previous db (elastic). So changing to CouchDb is not only about writing new implementation of couch, I also have to change V and C layers.. That sucks! Awaiting topic is to make new separation of layers in the app.


How to make count? Using map&reduce paradigm.

1. Map
First create any view emitting one record per record :) I called it alldocs,

function(doc) {

 emit(doc._id,doc.timestamp);
}

2. Reduce

Thankfully there are some built-in reduce functions that we may use. One of them is _count. So the "code" needed in reduce function is _count

The result after pinging the url http://localhost:5984/thought/_design/alldocs/_view/alldocs is

{"rows":[
{"key":null,"value":19}
]}
How to use it in js app? Using couch.js client i just can only



db.view('alldocs/alldocs').rows[0].value

And that's it I got size of all docs. Voila

Brak komentarzy:

Prześlij komentarz