Here's my test case on JS Bin.
Imagine you have an IndexedDB object store. Your objects are very simple, with two properties "a" and "b". Both properties contain integers. And because you want to be able to query your object store for particular values of "a" and "b", you create a compound index. Here's some code that follows this description:
var db;
request = indexedDB.open("test", 1);
request.onerror = function (event) { console.log(event); };
request.onupgradeneeded = function (event) {
var db = event.target.result;
db.onerror = function (event) { console.log(event); };
var store = db.createObjectStore("store", {keyPath: "id", autoIncrement: true});
store.createIndex("a, b", ["a", "b"], {unique: true});
store.add({a: 0, b: 0});
store.add({a: 1, b: 0});
store.add({a: 1, b: 1});
};
request.onsuccess = function (event) {
db = request.result;
db.onerror = function (event) { console.log(event); };
};
That works fine if you want to retrieve a particular object from your database. So if you run this:
db.transaction("store").objectStore("store").index("a, b").get([1, 0]).onsuccess = function (event) { console.log(event.target.result); };
the output is:
Object {a: 1, b: 0, psid: 2}
Wonderful. Great. So now you want to retrieve a range of values. Say, you want to fix "b" to be 0 while letting "a" be 0 or 1. Now weird stuff starts to happen. Try this code:
console.log("[a, b] bound from [0, 0] to [1, 0]");
db.transaction("store").objectStore("store").index("a, b").openCursor(IDBKeyRange.bound([0, 0], [1, 0])).onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
console.log(cursor.value);
cursor.continue();
} else {
console.log("[a, b] bound from [0, 0] to [2, 0]");
db.transaction("store").objectStore("store").index("a, b").openCursor(IDBKeyRange.bound([0, 0], [2, 0])).onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
console.log(cursor.value);
cursor.continue();
}
};
}
};
It produces this output:
[a, b] bound from [0, 0] to [1, 0]
Object {a: 0, b: 0, id: 1}
Object {a: 1, b: 0, id: 2}
[a, b] bound from [0, 0] to [2, 0]
Object {a: 0, b: 0, id: 1}
Object {a: 1, b: 0, id: 2}
Object {a: 1, b: 1, id: 3}
Bounding between [0, 0] and [1, 0] seems to work fine. But bounding between [0, 0] and [2, 0] does not! It also returns [0, 1] as a result. Why does [1, 0] work as an upper bound but [2, 0] doesn't? I feel like I must be either making a really stupid mistake or very fundamentally misunderstanding something.
And in case you missed it, here is the link to this on JS Bin again.
EDIT for Josh: I originally only tried it with just bound(x,y)
which is the same as bound(x,y,false,false)
. Changing it to bound(x,y,true,true)
can be seen here. Output is
[a, b] bound from [0, 0] to [1, 0]
[a, b] bound from [0, 0] to [2, 0]
Object {a: 1, b: 0, id: 2}
Object {a: 1, b: 1, id: 3}
I still don't have a good intuitive understanding for how this stuff makes sense.