Need help understanding query results

23 views
Skip to first unread message

Yonah Forst

unread,
Nov 19, 2015, 4:46:50 AM11/19/15
to Couchbase Mobile

I'm getting query results that I don't understand.

I have a view to find a user's sent friend requests. It emits the following keys: [fromUUID, toUUID, updatedAt]. Then I create a query and set the start key to the user's UUID (myUUID), and the end key to an array: [myUUID, {}, {}]


I'm running into some weird cases. For example, if i set the start key to BCEE54E and the end key to [BCEE54E, {}, {}] I'll get results with keys like [A2A7B92, BCEE54E, 2015-11-18T11:35:16Z]


I would have thought that it would not include this key since the first string doesn't match.


i've posted the java code here: https://gist.github.com/joshblour/3fb6bc5aa0a5a4c72e20 

the results are in the comments

Mike Rhodes

unread,
Nov 19, 2015, 8:39:11 AM11/19/15
to Couchbase Mobile
At least on normal CouchDB, this would probably be view collation hitting you. Your string sorts lower than an array containing any string as first element. Try setting the start key as `[BCEE54E`], that is, in an array.

Mike.

Yonah Forst

unread,
Nov 19, 2015, 9:22:11 AM11/19/15
to Couchbase Mobile
I've tried with the start key as an array and get the same results

Jens Alfke

unread,
Nov 20, 2015, 12:58:47 PM11/20/15
to mobile-c...@googlegroups.com

> On Nov 19, 2015, at 6:22 AM, Yonah Forst <yonah...@gmail.com> wrote:
>
> I've tried with the start key as an array and get the same results

Could you show an example? Perhaps showing the code that creates the query?

--Jens

Yonah Forst

unread,
Nov 20, 2015, 1:08:43 PM11/20/15
to Couchbase Mobile

 Sure. Here's the iOS code sample. the original Java sample is here: https://gist.github.com/joshblour/3fb6bc5aa0a5a4c72e20
    CBLView* view = [db viewNamed: @"all_friend_requests_by_sender"];

   
if (!view.mapBlock) {

       
// Register the map function, the first time we access the view:

       
[view setMapBlock: MAPBLOCK({

           
if ([doc[@"type"] isEqualToString:kFriendRequestDocType]) {

                emit
(@[doc[@"fromUUID"], doc[@"toUUID"], doc[@"updatedAt"]], doc[@"status"]);

           
}

       
}) reduceBlock: nil version: @"2"]; // bump version any time you change the MAPBLOCK body!

   
}




   
CBLQuery *query = [view createQuery];

    query
.startKey = @[[self myUUID]];

    query
.endKey = @[[self myUUID], @{}, @{}];

Jens Alfke

unread,
Nov 20, 2015, 2:24:13 PM11/20/15
to mobile-c...@googlegroups.com
That looks correct, although you only need a single “@{}” in the endKey array.

The incorrect results you showed in the first post were a result of not wrapping myUUID in an array in the startKey. With the current query code, what are the results you get that seem incorrect?

—Jens

Yonah Forst

unread,
Nov 23, 2015, 11:33:38 AM11/23/15
to Couchbase Mobile
With the start key as an array it now works perfectly :) I must have messed up somewhere the first time I tried it.

Thanks for your help, Jens and Mike.
Reply all
Reply to author
Forward
0 new messages