ViewQuery startKey & endKey

I’m struggling fetching documents with keys which start or end with some given value.
Here’s the scenario.

In CB it looks like this
key -> document

1;1 -> { }
2;1 -> { }
3;1 -> { }
4;2 -> { }
5;2 -> { }
6;2 -> { }

what I would like to do is to readout all documents which end with "2" that is:

4;2 -> { }
5;2 -> { }
6;2 -> { }

or all documents which end with "1"

1;1 -> { }
2;1 -> { }
3;1 -> { }

same for startKey if I query for key which starts with “1” as a result I would expect to get 1;1 -> { } only.

What happens now is that when I do

ViewQuery query = ViewQuery.from(“beer”, “beer”).startKey(“1;”);

as a result I’m getting each and every document I’ve got.

@s1awek , you can’t use startKey and endKey attribute with ‘Composite Key’, startKey and endKey with work if you create View with single key. like

1 → { }
1 → { }
1 → { }

@vinod did I mention CompositeKey anywhere? Doubtful. That class doesn’t even exists in new API. I got string keys with semicolon in them - single value with special character.

3 documents with same id, interesting, but wrong.

@s1awek, Example you mentioned in topic is for composite key

Can you try with single key?

What makes you think It’s for composite key? I said that keys I’m using are regular string values. Those string values have semicolon in them but that doesn’t make them an arrays or objects.
This is what I found in official doc.


question is, can I do the same for ID suffix?

as far as I know, no, you can’t do suffix querying… these sort of keys are sorted in an alphanumerical order, and the \u0000 is a trick, since this character will be considered the highest.

see the order of these example keys with a query like startKey=aa&endKey=aa\u0000

a
aa <--starts returning from this key (first matching or greater than startKey)>
aaa
aab
aac
aargh
aaz
aaza
aazz
aazzzzzzzz <--stops returning here (last before we don't match endKey anymore)>
aa{\u0000} <--endKey's last character is alphabetically higher than anything>
ab
aba
abba
dance

For prefix querying, you do have to provide an endKey. Otherwise couchbase will just (conceptually) sort the keys, skip until it finds the matching startKey (or the existing key immediately after that alphabetically) and then return all the following keys…

PS: the fact that your keys were simple strings with a semicolon in them was not terribly obvious in your initial post :confused:

You’re right, I should put those keys in my example in quotes. Thanks for answering my question. I was afraid it’s not going to work the way I expect it to.

If my Couchdb data like below, how can i query the namespace==aaaaa only? (btw, it will not return namespace=aaaaa-dev data)

{
  "_id": "aaaaa/hello1",
  "_rev": "11-e389d9665a88d17846202089f1d69cf2",
  "name": "hello1",
  "namespace": "aaaaa"
}
{
  "_id": "aaaaa/hello2",
  "_rev": "12-e389d9665a88d17846202089f1d69cf2",
  "name": "hello2",
  "namespace": "aaaaa"
}
{
  "_id": "aaaaa-dev/hello1",
  "_rev": "13-e389d9665a88d17846202089f1d69cf2",
  "name": "hello1",
  "namespace": "aaaaa-dev"
}
{
  "_id": "aaaaa-dev/hello2",
  "_rev": "14-e389d9665a88d17846202089f1d69cf2",
  "name": "hello2",
  "namespace": "aaaaa-dev"
}

@s1awek can you have a look at it?