How to access Couchbase ViewResults in golang?

I’m developing a server in golang with Couchbase (using the new Couchbase Go SDK). Unfortunately, I’ve hit a roadblock and cannot find any complete examples to reference.

When you query a view, how do you access:

  1. the total number of rows returned?
  2. access the key/value fields in the row returned by Next()

Seems like it should be obvious. Here is a snippet I’m using based on the example at:

myQuery := gocb.NewViewQuery(“tokens”, “blacklist_tokens”)
rows := bucket.ExecuteViewQuery(myQuery)

// ??? How do you get the result (row) count, here?

var row interface{}
for rows.Next(&row) {
fmt.Printf(“Row: %+v\n”, row) // ??? How do your access the key and value fields, here?
if err := rows.Close(); err != nil {
fmt.Printf(“View query error: %s\n”, err)

Thanks for any help with this.

Here is a good example:


Todd Greenstein

Thank you, that helps.

Is there any way to get the row.count without looping/counting? viewResponse in bucket_http.go has this value, but it gets hidden.

The reason it’s hidden is that the count there is the count of everything in the view-- not just the items that matched the query. We know what’s not what people expect it to mean, so we don’t usually expose it That’s what the server returns to the client.

Since the server doesn’t return the count, if the SDK were to do it for you it’d be approximately the same as counting the results in your code. It’s a bit more work, but there’s no performance advantage.

This does come up often enough that I’ve filed an RFE to add the count of the results. You can track it in the issue tracker if you’d like.

Thanks for the response Matt – and RFE.

No problem. Track the RFE for status on this please.

For now, you’re best to count the results in the application.