Hi all, I am a couchbase newbe and try to group documents by ‘categoryName’.
My documents look like:
{
"id": "project1",
"title": "project1_title",
"detailPageUrl": "url for project 1 detail page",
"categoryName": "Category A",
}, {
"id": "project2",
"title": "project2_title",
"detailPageUrl": "url for project 2 detail page",
"categoryName": "Category B",
}
...
I read the documentation about views query map and reduce but still not understand how to group the documents by ‘categoryName’. I just find a way to get all possible categories using:
let map: CBLMapBlock = { (doc, emit) in
if (doc[typeKey] != nil && doc[typeKey] as! NSString == "project") {
emit(doc["id"]!, doc["categoryName"])
}
}
let reduceBlock:CBLReduceBlock = { (keys, values, rereduce) -> Any! in
let set = Set<String>(values as! [String])
return Array(set)
}
view.setMapBlock(map, reduce: reduceBlock, version: "1")
let categories = (data.allObjects[0] as! CBLQueryRow).value as! [String]
I hope to find a way to group them by that property to finally create a dictionary with the category as key and an array withe the documents belonging to this category as value. Does anybody know how to solve that and it is possible at all?