Querying a compound key from a view


#1

Ok my keys looks like that

key = [ 1404891029233,“batman” ]
key = [ 1404891029283,“superman” ]
key = [ 1404898029283,“spiderman” ]

i want to do a range query ,
to get all the “superman” records ?

query =>
startkey=[0,“superman”]
endkey=[999999999999999,“superman\u02ad”]

i guess I’m missing something.


#2

The way start and end key work in couchbase is by doing a prefix match, so imagine in you example the following documents

hero1 = { type: "hero", name: "superman", number: 1 } hero2 = { type: "hero", name: "spiderman", number: 2 } hero3 = { type: "hero", name: "batman", number: 3 } hero4 = { type: "hero", name: "superman", number: 4 }

when we index them by creating a key via their name an their number in the way of creating them like

[1,“superman”], [2,“spiderman”], [3,“batman”], [4,“superman”]

when doing a query using a prefix match on startkey = [1,“superman”], endkey = [9,“superman”] will actually not give you all the superman documents but all the documents, because [3,“batman”] is key ordering wise below [4,“superman”] because 3 < 4 and we only do a prefix match.

What you would need to do is inverse the key [“superman”, 1], [“superman”, 4], [“batman”,3],…. Now if you query for prefix [“superman”] you will get both superman results.

I created a gist to illustrate