startKeyDocID and endKeyDocID question

17 views
Skip to first unread message

Herman Chan

unread,
Oct 20, 2016, 11:51:50 AM10/20/16
to Couchbase Mobile
Hi there,

Here is the code:

   

CBLQuery *query = [someView createQuery]

 

    query
.prefetch = YES;

    query
.startKeyDocID =someDoc.documentID;

    query
.endKeyDocID = someDoc.documentID;

    query
.limit = 1;

     

   
NSMutableArray *rows = [NSMutableArray array];

   
NSError *__autoreleasing error;

   
for (CBLQueryRow *row in [query run:&error]) {
           
[rows addObject:row];
   
}


I expected rows to contain only 1 row which is the document id that I am looking for, but how it returns all rows from the query. I can filter out the documentID from the result, but it seems suboptimal.


Is there anyway just grab one document from a query?


Herman 


Jens Alfke

unread,
Oct 20, 2016, 1:28:54 PM10/20/16
to mobile-c...@googlegroups.com
start/endKeyDocID don’t do anything if you haven’t also set start/endKey. Their purpose is only to disambiguate between multiple rows with the same key.

For example, if you’re paging the results of a query you’d set the limit to (say) 100, and the startKey to the key of the last row from the previous query. But this breaks down if there are a ton of rows with that same key; you’ll start over at the first one, so if there are more than 100 rows with that key, you get into an infinite loop. The solution is to also set startKeyDocID to the docID of the last row of the previous query. That way you start over from that row.

—Jens

Herman Chan

unread,
Oct 20, 2016, 1:53:48 PM10/20/16
to Couchbase Mobile
Thanks Jens,

Ok, interesting, the key of the query I am calling consists of either "nil" or some UUID string, what should my startKey and endKey be in this case if I want to just get one document out of that query?

Herman

Herman Chan

unread,
Oct 20, 2016, 2:29:56 PM10/20/16
to Couchbase Mobile
nvm, found out that I can just set startKey to nil.

Thanks,
Herman

Jens Alfke

unread,
Oct 20, 2016, 3:50:56 PM10/20/16
to Couchbase Mobile

On Oct 20, 2016, at 10:53 AM, Herman Chan <herm...@gmail.com> wrote:

Ok, interesting, the key of the query I am calling consists of either "nil" or some UUID string, what should my startKey and endKey be in this case if I want to just get one document out of that query?

Set `keys` to just the one UUID you want, and limit=1.

(Or you could set startKey = endKey = the UUID.)

—Jens

Herman Chan

unread,
Oct 20, 2016, 3:54:13 PM10/20/16
to Couchbase Mobile
sorry, the key for each row of that query can be either nil or some uuid which is not the document id.

I think I've got it with setting startkey = nil and it seems to work, please let me know if that's not going to work.

Herman
Reply all
Reply to author
Forward
0 new messages