how can group like sql

38 views
Skip to first unread message

amir nikfar

unread,
Apr 21, 2014, 5:34:32 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

for example 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 

Jens Alfke

unread,
Apr 21, 2014, 5:54:22 PM4/21/14
to mobile-c...@googlegroups.com

On Apr 21, 2014, at 2:34 PM, amir nikfar <nikipro...@gmail.com> wrote:

> 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

To group together all the rows with the same key, set the groupLevel property of the query to 1. The result will give you one row for each unique key, with a nil value.

If you want something in the value, like the count, you’ll need to add a reduce function. The reduceBlock would look like
REDUCEBLOCK({ return @(values.count); })

—Jens

amir nikfar

unread,
Apr 21, 2014, 6:33:32 PM4/21/14
to mobile-c...@googlegroups.com
thanks a lot

but i have another same question

when i want to search some text in all my note(the text property of note NOT the name of note) and group the result by the "name property" and look like the before question i want to know the count  of each group what to do?
in sql something like this : 
select count(*),name from notes when text like '%cat%' group by name;
my code like this:


NSString *text = @"cat";

CBLView* view = [database viewNamed: @"fullTextSearch_on_note"];

    if (!view.mapBlock) {

        [view setMapBlock: MAPBLOCK({

            if ([doc[@"doc_type"] isEqualToString: @"note"]) {

                id date = [doc objectForKey: @"created_at"];

                NSString* body = doc[@"text"];

                //emit(body,@[date,doc]);

                emit(CBLTextKey(body),@[doc["name"],date]);

            }

        }) reduceBlock: REDUCEBLOCK({

            return @(values.count);

        })

            version: @"5"]; // bump version any time you change the MAPBLOCK body!

    }

    


    CBLQuery* query = [view createQuery];

    query.fullTextQuery = text;

    query.groupLevel = 1;


    NSError *err;


    CBLQueryEnumerator *allDocument = [query run:&err];



but the row of allDocument is zero (no result)

Jens Alfke

unread,
Apr 21, 2014, 8:01:29 PM4/21/14
to mobile-c...@googlegroups.com

On Apr 21, 2014, at 3:33 PM, amir nikfar <nikipro...@gmail.com> wrote:

when i want to search some text in all my note(the text property of note NOT the name of note) and group the result by the "name property”

Use full-text indexing. You can’t do grouping with this, though.

—Jens
Reply all
Reply to author
Forward
0 new messages