sql group query with map block and reduceBlock

23 views
Skip to first unread message

amir nikfar

unread,
Apr 21, 2014, 5:52:24 PM4/21/14
to mobile-c...@googlegroups.com
hi dear Jens Alfke
i new user in NOSQL and i find couchBase.
in one of my project i want to show my notes document with type = "note"
i can do that with this code :

    CBLView* view = [database viewNamed@"note"];
    if (!view.mapBlock) {
        [view setMapBlockMAPBLOCK({
            if ([doc[@"doc_type"isEqualToString@"note"]) {
                id date = [doc objectForKey@"created_at"];
                NSString* body = doc[@"text"];
                emit(body,date);
            }
        }) reduceBlocknil version@"1"]; 
    }
    CBLQuery* query = [view createQuery];
    NSError *err;
    CBLQueryEnumerator *allDocument = [query run:&err];

this is work fine and and the allDocument have my array of note and it is ok
but i want to group my note with their name and count of each group
in sql this is something like this: SELECT count(*),name FROM note group by name
but in couch base i don't know how can i do that.
the note document is one CBMODEL like this:

#import <CouchbaseLite/CouchbaseLite.h>

@interface note : CBLModel

@property (readonlyNSString *doc_type;
@property (readonlyNSString *name;
@property (readonlyNSString *text;
@property (strongNSDate *created_at;

-(instancetype)initInDatabase:(CBLDatabase *)database withType:(NSString *)type withname:(NSString *)name withtext:(NSString *)text;

@end

i want to give answer with NSLog like this
name of note :cat      count of note:12
name of note:dog     count of note:28

please help me
i confuse
thanks a lot 
Reply all
Reply to author
Forward
0 new messages