Some context - 

I have a messaging feature in my app and I am trying to do a basic 
implementation of a query to map and reduce to the conversations with a 
particular user say (X) ordered by timestamp. 
Now in basic terms the logic obv. is - 

if ((from is me AND to is X ) OR (from is X and to is me)  //Fairly simple 
but we can't use it in the map function obviously 

My map function is mentioned below, in which we map only messages which are 
mine irrespective of any other user. 

Now I am confused about how to implement the reduce function so I get 
messages in which => either from is X OR to is X 
Any help on this would be really appreciated.

It's not very obvious to me coming from relational db background. 
May be I am doing something fundamentally wrong?


+ (CBLQuery*) queryMessagesInDatabase: (CBLDatabase*)db {
    CBLView* view = [db viewNamed: @"messages"];
    if (!view.mapBlock) {
        // Register the map function, the first time we access the view:
        [view setMapBlock: MAPBLOCK({
            id to = [doc objectForKey:@"to"];
            id from = [doc objectForKey:@"from"];
            id msg = [doc objectForKey:@"message"];
            id fromName = [doc objectForKey:@"fromName"];
            id timestamp = [doc objectForKey:@"timestamp"];
            NSString *myuid = [[NSUserDefaults standardUserDefaults] 
valueForKey:@"myuid"];

            if(to && from && msg && fromName && timestamp){
                if([to isEqualToString:myuid]||[from isEqualToString:myuid])
                {
                    emit(@[timestamp,from,to],nil);
                }
                
            }
        }) reduceBlock: nil version: @"1.1"]; // bump version any time you 
change the MAPBLOCK body!
    }
    return [view createQuery];
}

-- 
You received this message because you are subscribed to the Google Groups 
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mobile-couchbase/dfb0fb47-b6bf-49a5-bc95-93e018b8b3e2%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to