Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

queryByView and reduce functions in views #111

Open
iuliandavid opened this issue Oct 22, 2019 · 2 comments
Open

queryByView and reduce functions in views #111

iuliandavid opened this issue Oct 22, 2019 · 2 comments

Comments

@iuliandavid
Copy link

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 ?

@fabrijp
Copy link

fabrijp commented Nov 9, 2019

Have you tried using this parameter:
usingParameters: [.includeDocs(true)])

@iuliandavid
Copy link
Author

Retrieving all the documents it’s pretty expensive while a count or sum is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants