Re: TouchDB iOS examples with reduce function

280 views
Skip to first unread message

Jens Alfke

unread,
Nov 8, 2012, 1:23:07 PM11/8/12
to mobile-c...@googlegroups.com

On Nov 8, 2012, at 9:58 AM, Aleksey Dobrushin <alex.do...@gmail.com> wrote:

> It it correct? How do I call it ro get actually rows count?

It looks correct to me. If you query the view, by calling [design queryViewNamed: @“casesByCreateDateCount”], you’ll get back a single row of results, whose value is the count.

—Jens

Aleksey Dobrushin

unread,
Nov 8, 2012, 1:32:09 PM11/8/12
to mobile-c...@googlegroups.com
Unfortunately, I receive all documents, as there is no REDUCE block defined...


        CouchQuery* query = [design queryViewNamed: @"casesByCreateDateCount"];

        for (CouchQueryRow* row in query.rows) {

            NSLog(row.documentID);

            id value = row.value;

            NSDictionary* content = [row.document properties];

            for (id key in content) {

                id value = [content objectForKey:key];

                NSLog(@"key: %@, value: %@", key, value);

Aleksey Dobrushin

unread,
Nov 9, 2012, 4:59:51 AM11/9/12
to mobile-c...@googlegroups.com
I've played with this last night.
There is a groupLevel property, if I set this property to 1 grouping works fine, and each of the result rows contain different emitted key, the value actually is the result of reduce function. 
So I can use grouping to get count of cases, if I emit for each case always the same key, like this:

[design defineViewNamed: @"casesByCreateDateCount" mapBlock: MAPBLOCK({
        id date = [doc objectForKey: @"createDate"];
        NSString* docType = [doc objectForKey: @"type"];
        if(date && docType && [docType isEqualToString:@"case"])
            emit(@"casesByCreateDateCount", NULL);
    }) reduceBlock: REDUCEBLOCK({
        return [NSNumber numberWithInt:values.count];
    }) version: @"1.0"];

CouchQuery* query = [design queryViewNamed: @"casesByCreateDateCount"];
[query setGroupLevel:1];
for (CouchQueryRow* row in query.rows) {
      id rowCount = row.value;
       NSLog(@"Number of cases: %@", rowCount);
}

I still don't know how good or bad is it in for performance.
I still don't understand, why REDUCE block is ignored if I trying to get count as it described in all NoSQL manuals. 

Reply all
Reply to author
Forward
0 new messages