Skip to content

queryByView and reduce functions in views #111

@iuliandavid

Description

@iuliandavid

I have the following view :

let design = DesignDocument(_id: "_design/mydesign", views: [
            "count_doc": [
               "map": "function(doc) { if(doc.type == 'mydoc') { emit(doc._id, 1); } }",
                "reduce": "_count"
            ]
])

in version 2.1.0 I was able to get the result with the following code:

database.queryByView("count_doc", ofDesign: "mydesign", usingParameters: []) { (doc, err) in
            if let doc = doc, err == nil {
                if let count = doc["rows"][0]["value"].int {
                    completion(count, nil)
                } else {
                    completion(0, nil)
                }
            } else {
                completion(nil, err)
            }
        }

Now in the current version:

database?.queryByView("count_doc", ofDesign: "mydesign", usingParameters: []) { (doc, err) in
            if let doc = doc, err == nil {
                if let count = doc.total_rows {
                    completion(count, nil)
                } else {
                    completion(0, nil)
                }
            } else {
                completion(nil, err)
            }
        }

I get the error

Failed to decode AllDatabaseDocuments from response

which is correct since the result is something like
{"rows":[ {"key":null,"value":26} ]}
Can you tell me how to retrieve the result from this view or a view that contains "_stats" function ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions