Indexed DB cursor ranges on multiple properties
The index you created is a composite index. It is query like this:
index = objectStore.index('treelocation');
index.get([123456, 654321]);
Alternatively, you can use two indexes for each coorrd. In my opinion it is better.
x_index = objectStore.index('xcoord');
y_index = objectStore.index('ycoord');
x_species = x_index.get(IDBKeyRange.only(123456))
y_species = y_index.get(IDBKeyRange.only(654321))
species = x_species.intersect(y_species); // this is result
I have been told the solution is indeed IDBKeyRange.bound([lowX,lowY],[highX,highY])
.
The range suggestion is part of the answer, but even with array keys it really is just a 1-dimensional range, not a 2- (or N-) dimensional selection of the data. With your current schema you'll need to do something like this:
index.openCursor([lowX, lowY], [highX, highY]).onsuccess = function(e) {
var cursor = e.target.result;
if (!cursor) return; // done!
var x = cursor.key[0], y = cursor.key[1];
// assert(lowX <= x && x <= highX);
if (y < lowY) {
cursor.continue([x, lowY]);
} else if (y > highY) {
cursor.continue([x + 1, lowY]);
} else {
processRecord(cursor.value); // we got one!
cursor.continue();
}
};
(if coordinates are not integral, replace + 1
with an appropriate epsilon)
I've posted an example of a generalized solution to:
https://gist.github.com/inexorabletash/704e9688f99ac12dd336