0

I did .createIndex('abcIDX', ['A', 'B', 'C']).

However, I felt the results were strange, so I did the following tests.

// All methods except "IDBKeyRange.only()" are weird.;
const range = IDBKeyRange.lowerBound( [5,5,5] );

function test( arr ) {
  console.log( range.includes(arr) );
}

test([5,5,5]); // case1: true
test([5,5,1]); // case2: false
test([5,6,1]); // case3: true ?
test([6,1,1]); // case4: true ??
test([1,6,1]); // case5: false ??

I thought IDBKeyRange works as A && B && C.

Cases 1 and 2 were right.

But cases 3 and 4 were not. So I guessed it works with ||, not &&.

However, cases 2 and 5 showed that the speculation was wrong.

Stackoverflow had a great answer. Here's what I understand after reading this answer.

It's work A || (A && B) || (A && B && C).

However, case 2 showed that the speculation was wrong.

What am I wrong with? Or am I using it wrong? Help me plz...

msm082919
  • 617
  • 8
  • 24
  • Does this answer your question? [In IndexedDB, is there a way to make a sorted compound query?](https://stackoverflow.com/questions/12084177/in-indexeddb-is-there-a-way-to-make-a-sorted-compound-query) – Josh May 30 '21 at 16:49

1 Answers1

1

The comparison may be easier to understand if you map numbers to characters, and think of string comparisons. A=1, B=2, E=5, F=6. So [5,5,5] is "EEE"

range = IDBKeyRange.lowerBound( [5,5,5] ); // think of this as "EEE"

test([5,5,5]); // case1: "EEE" >= "EEE" ? true
test([5,5,1]); // case2: "EEA" >= "EEE" ? false
test([5,6,1]); // case3: "EFA" >= "EEE" ? true
test([6,1,1]); // case4: "FAA" >= "EEE" ? true
test([1,6,1]); // case5: "AFA" >= "EEE" ? false

The actual logic is basically:

((test[0] >  range[0])) ||
((test[0] == range[0]) && (test[1] >  range[1])) ||
((test[0] == range[0]) && (test[1] == range[1]) && (test[2] >= range[2]))
Joshua Bell
  • 7,727
  • 27
  • 30
  • Thank you. understand. So, is it not possible to multi-query each field A, B, C where numeric values are stored? Cases 3 and 4 are still weird when the range of values needs to be numbers. :( – msm082919 Feb 10 '21 at 03:35
  • Correct. Key space is linear. You may find https://gist.github.com/inexorabletash/704e9688f99ac12dd336 helpful. – Joshua Bell Feb 10 '21 at 17:54
  • Thank you for your kind and detailed answer. :) – msm082919 Feb 11 '21 at 13:49