Using group_level

16 views
Skip to first unread message

stamhaney

unread,
Apr 9, 2015, 2:05:37 PM4/9/15
to mobile-c...@googlegroups.com
Hi ,

I have a collection of documents where each document has the fields (besides others):  parent_project_number and project number.  Documents which have the same parent_project_number, are sub projects of the document with the parent_project_number. So e.g.

parent_project_number   project_number
------------------------------    ---------------------
5                                         1
5                                         2
5                                         3
6                                         4
6                                         8

I would like to order the document collection so that I get a list of project_numbers for each parent_project_number (as shown above)  using a compound key of [main_project_number, project_number].  Would I need to use group_level, in order to achieve this order? Does using group_level need a reduce block? What would the reduce block contain? Can you please give me a code example? I am using the following query but I am only getting nulls:

    [projectsView setMapBlock: MAPBLOCK({

        if ([doc[@"type"] isEqualToString: @"projects"]) {            

            id main_project_numberid = doc[@"main_project_number"];

            id project_numberid = doc[@"project_numberid"];

            emit(@[main_project_numberid, project_numberid],  doc[@"project_numberid" );

        }

    }) version: @"1"];

    CBLQuery *query = [projectsView createQuery];

    query.grouplevel = 2;

   CBLQueryEnumerator* result = [query run: &error];

Thanks

Jens Alfke

unread,
Apr 9, 2015, 2:17:38 PM4/9/15
to mobile-c...@googlegroups.com
I gave a talk last year that went into detail on doing this:

The main problem with the code you showed is that groupLevel should be 1, not 2.

—Jens

stamhaney

unread,
Apr 10, 2015, 1:36:34 AM4/10/15
to mobile-c...@googlegroups.com
Thanks a Ton Jens!
Reply all
Reply to author
Forward
0 new messages