I'm a total newbie with NoSQL DBs in general, and with Couchbase in particular. My main document type is a Session type. A Session document has a startDate, an endDate and an owner field.
Several sessions can be owned by the same user. A session is closed if it has an endDate. On one given device, a given user can only open a Session if it doesn't already have one open. But they can open a session on another device, which means I can have several open sessions in my database for any given owner. I want to retrieve the most recently open session for a given owner. I'm pretty sure it can be done with a clever map/reduce view, but I'm sill confused about how it works. Here is what I started doing in Objective-C:
[[database viewNamed:@"openByOwner"] setMapBlock:^(NSDictionary *doc, CBLMapEmitBlock emit) {
if([doc[@"type"] isEqualToString:@"session"] && doc[@"endDate"] == nil){
emit(doc[@"owner"], doc);
}
} reduceBlock:^id(NSArray *keys, NSArray *values, BOOL rereduce) {
return nil;
} version:@"1.0"];With this I think I can filter my documents to get only open sessions, but how should I proceed to get only the one with the biggest start date for each owner in my query?