[Views] startkey_docid doesn't work except for _all_docs

Hello,

The startkey_docid view parameter doesn’t seam to have any effect on views.
It only works with the “undocumented/unsupported” _all_docs view each time we test it

Example :

  • Create the default bucket on a fresh couchbase cluster/server

  • Insert 3 keys : key1, key2, key3 (any value is fine)

  • Query the _all_docs view with no startkey_docid :

    curl http://127.0.0.1:8091/couchBase/default/_all_docs
            {"total_rows":3,"rows":[
            {"id":"key1","key":"key1","value":{"rev":"1-0008e9b571d0e8d10000000000000000"}},
            {"id":"key2","key":"key2","value":{"rev":"1-0008e9b845ed09e20000000000000000"}},
            {"id":"key3","key":"key3","value":{"rev":"1-0008e9ba1da51ce60000000000000000"}}
            ]
            {}
    
  • Query the _all_docs view with startkey_docid = key2 : startkey_docid works as expected

            curl http://127.0.0.1:8091/couchBase/default/_all_docs?startkey_docid=key2
            {"total_rows":3,"rows":[
            {"id":"key2","key":"key2","value":{"rev":"1-0008e9b845ed09e20000000000000000"}},
            {"id":"key3","key":"key3","value":{"rev":"1-0008e9ba1da51ce60000000000000000"}}
            ]
            }
    
  • Create a custom view that mimics the all _all_docs view (simple Map, no Reduce) :

            function (doc, meta) {
              emit(meta.id, {"rev": meta.rev});
            }
    
  • Query this custom view with no startkey_docid :

            curl 'http://127.0.0.1:8092/default/_design/dev_myView/_view/myAll_docs'
    
            {"total_rows":3,"rows":[
            {"id":"key1","key":"key1","value":{"rev":"1-0008e9b571d0e8d10000000000000000"}},
            {"id":"key2","key":"key2","value":{"rev":"1-0008e9b845ed09e20000000000000000"}},
            {"id":"key3","key":"key3","value":{"rev":"1-0008e9ba1da51ce60000000000000000"}}
            ]
            }
    
  • Query this view with startkey_docid = key2 : startkey_docid doesn’t work as expected

            curl 'http://127.0.0.1:8092/default/_design/dev_myView/_view/myAll_docs?startkey_docid=key2'
    
            {"total_rows":3,"rows":[
            {"id":"key1","key":"key1","value":{"rev":"1-0008e9b571d0e8d10000000000000000"}},
            {"id":"key2","key":"key2","value":{"rev":"1-0008e9b845ed09e20000000000000000"}},
            {"id":"key3","key":"key3","value":{"rev":"1-0008e9ba1da51ce60000000000000000"}}
            ]
            }
    

Are we missing something with the way it should work ?
The Doc states it is much efficient to use startkey_docid rather than startkey (and we are sure it is), but what is the proper way to use it on custom views ?

We have tested it on different couchbase cluster, on different platforms (centOS, uBuntu, OSX), with different custom views, including reduce or not, and it never gives the expected output.

We are currently running on v2.1.1 CE (we think we tried it with every couchbase CE version since 2.0 with the same behaviour)

Regards,

Xavier

Hello,

I am under the impression you are mixing the startkey/endkey parameters that are used to deal with range queries, with the startkey_docid/endkey_docid that are used for pagination.

I am inviting you to look at this blog post:

Let me know if you have more questions, and I will also ensure we clarify the documentation.

Regards
Tug
@tgrall

Ok, thank you : it seams we just didn’t read this part of the doc :

When using startkey_docid you must specify the startkey parameter to specify the information being searched for.

We thought we could use startkey_docid the same way as the _all_docs view <- with no startkey

By the way, The use of the startkey_docid returns output that is pretty much unpredictable when you want to use it for something that is not “pagination related”; Ex :

Imagine you have the following items in your bucket :

key value
key1 {“groupKey”:“groupA”}
key2 {“groupKey”:“groupA”}
key3 {“groupKey”:“groupA”}
key4 {“groupKey”:“groupB”}
key5 {“groupKey”:“groupB”}
key6 {“groupKey”:“groupB”}
key7 {“groupKey”:“groupC”}
key8 {“groupKey”:“groupC”}
key9 {“groupKey”:“groupC”}

with the view :

function (doc, meta) {
emit(doc.groupKey, null);
}

The following view call is pretty much unexpected :

?startkey=“groupA”&startkey_docid=key7

{“total_rows”:9,“rows”:[
{“id”:“key4”,“key”:“groupB”,“value”:null},
{“id”:“key5”,“key”:“groupB”,“value”:null},
{“id”:“key6”,“key”:“groupB”,“value”:null},
{“id”:“key7”,“key”:“groupC”,“value”:null},
{“id”:“key8”,“key”:“groupC”,“value”:null},
{“id”:“key9”,“key”:“groupC”,“value”:null}
]
}

The doc states : “startkey_docid Return records starting with the specified document ID”

startkey_docid was set to key7, but the view output starts at key4 (??)

Xavier

Can somebody of Couchbase give Xavier an explanation?
We are also trying to understand the use of startkey_docid parameter for pagination and we encounter the same issues!

We have a simple view:

function (doc, meta) {
if (doc.doc_type && doc.doc_type == ‘circle’) {
emit(doc.circle_type_code);
}
}

…/bucketName/_design/dev_circle/_view/byCircleTypeTsUpd?stale=ok&skip=0&reduce=false

The result is as expected = 130 rows, extract:

{“total_rows”:130,“rows”:[
{“id”:“IL::circle::049a13be-cbd5-4ef9-b45c-1b17d539ac7f”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::085e3c4a-8899-4198-bd4f-4a6b80fb2b53”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::0c3bdd8b-0221-44ef-b4ff-585f7589b3a7”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::0d070bbb-9ee7-40e1-ac72-2eaea515b941”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::0d16ac38-9163-403d-b0c1-d13b7add70d5”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::0fcc9f5a-c2a9-43ea-8a71-bcaad3d2cf78”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::13c42481-487a-4760-b499-a195ebd3a7af”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::14490227-c653-447b-9dc1-f8d2831829fb”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::18d9cfdf-6510-4d1d-8abb-2073f5cd8a96”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::1d201301-5b4e-493e-966c-5bab718e0642”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::1f344086-78d9-4aad-8ab1-7e69e0868c9d”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::1f63b32f-8eed-4f04-9507-a583d5f97c06”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::208c5f17-fb0a-4254-b8d9-c7cce8998df7”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::253d0581-9cb3-4eb8-aa3d-9becbe7b4a80”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::256cfafb-cf0e-4a37-b14d-b47a19e089d9”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::257efa6f-730b-415c-bc34-c6970532a183”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::290adbf4-5de8-45f7-a84b-f765a38095e2”,“key”:“cmps”,“value”:null},
{“id”:“IL::circle::290b7905-aeaa-425f-b898-2cfdcea350fd”,“key”:“cmps”,“value”:null},

now if we add the id of the 4th row as the startkey_docid, should the result not start at the 4th row??

…/bucketName/_design/dev_circle/_view/byCircleTypeTsUpd?stale=ok&skip=0&reduce=false&startkey_docid=“IL::circle::0d070bbb-9ee7-40e1-ac72-2eaea515b941”

The result is still the same 130 rows starting with the 1st row??

We thought maybe that startkey_docid parameter can only be used combined with a startkey so we run following url:

…/bucketName/_design/dev_circle/_view/byCircleTypeTsUpd?stale=ok&skip=0&reduce=false&startkey=“cmps”&startkey_docid=“IL::circle::0d070bbb-9ee7-40e1-ac72-2eaea515b941”

Not better?

We are using
Version: 2.2.0 community edition (build-837)

Your help will be appreciated

Thanks

Fred

I found my problem here:

To use startkey_docid you need to supply a startkey and DO NOT SURROUND your doc id with "
so this worked for me:
…/bucketName/_design/dev_circle/_view/byCircleTypeTsUpd?stale=ok&skip=0&reduce=false&startkey=“cmps”&startkey_docid=IL::circle::0d070bbb-9ee7-40e1-ac72-2eaea515b941

And to be more precise/complete: startkey_docid must be used together with the startkey which is the key of the index of the row with doc id = startkey_docid.

More info here:
see http://blog.couchbase.com/startkeydocid-behaviour