couchdb-golang icon indicating copy to clipboard operation
couchdb-golang copied to clipboard

total rows with views

Open jcantonio opened this issue 7 years ago • 5 comments

Hello, Thank you for this great library.

If the query is done directly on couchDB http://localhost:5984/mydb/_design/myview/_view/all?limit=1 I get the total rows = 6 which is number of rows for that view regardless of the limit.

If the query is done via the library

option := map[string]interface{}{"limit": 1}
results, err := myDB.View("myview/all", nil, option)
results.TotalRows() // returns 1

I get the total rows = 1 which is number of rows returned constrained by the limit.

Is it possible to get number of rows regardless of the limit? As without this I wonder how paging could be done. Cheers

jcantonio avatar Feb 14 '18 21:02 jcantonio

I see where it differs in design.go vr.totalRows = len(rowsRaw) it should be json.Unmarshal(*jsonMap["total_rows"], &vr.totalRows)

jcantonio avatar Feb 14 '18 22:02 jcantonio

@jcantonio Thank you for finding this bug, would you help me to fix it? : )

leesper avatar Feb 15 '18 01:02 leesper

@leesper Sure, I will create a pull request. Thanks

jcantonio avatar Feb 15 '18 07:02 jcantonio

@jcantonio Thank you very much, I really appreciate it!

leesper avatar Feb 15 '18 07:02 leesper

Found an issue if you try to do a use View(...) on a document with a reduce function. The returned raw JSON payload i get from couchdb:

{"rows":[
{"key":null,"value":12937}
]}

which causes an panic in design.go:154 when it tries to deref *jsonMap["total_rows"]


	var totalRows float64
	json.Unmarshal(*jsonMap["total_rows"], &totalRows)
	vr.totalRows = int(totalRows)

I'm not sure what a good fix would be except that having some null checks would avoid having the library panic. If I have the time, I will try to get back and figure out how to fix this issue is.

jaimeyu avatar Jul 05 '18 16:07 jaimeyu

there's no tag which fixes this issue, but on last commit in master this issued fixed. Workaround go get github.com/leesper/couchdb-golang@master

olegroom avatar May 10 '23 12:05 olegroom