Created
October 26, 2016 19:12
-
-
Save toddfreese/b147d5d84a30ff3e879a0922704a242d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I have this working CBL view that I need to reproduce as a CB view for a web client. We would like this over the public SG port | |
| if possible. | |
| CBLView *view = [self.document.database viewNamed: @"jobs/uniqueDayNumbersForJob"]; | |
| if (!view.mapBlock) { | |
| NSString *const kSceneDocType = [Scene docType]; | |
| [view setMapBlock: MAPBLOCK({ | |
| if ([doc[@"type"] isEqualToString:kSceneDocType]) { | |
| NSString *jobID = doc[@"job_id"]; | |
| NSString *sceneID = doc[@"_id"]; | |
| NSString *dayNumber = doc[@"dayNumber"]; | |
| if (jobID == nil) { | |
| jobID = @""; | |
| } | |
| emit(@[jobID, dayNumber, sceneID], dayNumber); // emit order is fixed because emit element is referred by index from calling method. | |
| } | |
| }) reduceBlock:REDUCEBLOCK({ | |
| return @(values.count); | |
| }) version: @"4"]; // bump version any time you change the MAPBLOCK body! | |
| } | |
| CBLQuery *query = [view createQuery]; | |
| query.groupLevel = 2; | |
| query.descending = NO; | |
| NSString *myJobId = self.document.documentID; | |
| query.startKey = @[myJobId]; | |
| query.endKey = @[myJobId, @{}]; | |
| return query; | |
| And I am trying to reproduce that same view in a CB view like this: | |
| { | |
| "views":{ | |
| "unique_days_for_scene":{ | |
| "map":"function (doc, meta) { if (doc.type == \"scene\") { emit([doc.job_id, doc.dayNumber, meta.id], doc.dayNumber); } }", | |
| "reduce":"function (key, values, rereduce) { return values.count; }" | |
| } | |
| } | |
| } | |
| Called this way: | |
| https://xxxxxx/shotbotdb/_design/unique_days_for_scene/_view/unique_days_for_scene?startkey=%5B%22-eMNk7zD8x_EQC66UVuP7QX%22%5D&endkey=%5B%22-eMNk7zD8x_EQC66UVuP7QX%7B%7D%22%5D&descending=false&group_level=2&reduce=true&stale=false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment