How do you query IndexedDB products by price range?

Tests IndexedDB indexing and range query APIs. You need a price index created in onupgradeneeded, then IDBKeyRange.bound(50, 100) with index.getAll or openCursor. Red flag: fetching all records and filtering in JavaScript.
What's really being asked
This question evaluates whether you treat IndexedDB as a structured database rather than a key-value dump. It checks your understanding of schema design through indexes, how ordered key ranges map to physical B-tree storage, and the tradeoffs between eager retrieval with getAll versus streaming with cursors. At a senior level the interviewer wants to hear about transaction modes, index lifecycle during upgrades, and memory pressure on large datasets.
The full answer
First, create an index on the price field inside the onupgradeneeded event using objectStore.createIndex with a name like priceIdx, the keyPath price, and unique set to false. Second, open a readonly transaction on the products store and access the index via store.index. Third, define the search space with IDBKeyRange.bound(50, 100, false, false) where the booleans control whether the endpoints are included. Fourth, choose the access pattern based on result size: use index.getAll(range) when you want an array immediately and the result set is small, or use index.openCursor(range) to yield records one by one and keep memory usage flat. Fifth, note that this approach avoids a full store scan because the engine walks only the index leaf nodes that fall inside the bound.
The mistakes people make
A critical red flag is iterating the entire object store with openCursor and filtering records in JavaScript, which forces a full table scan and ignores the index. Another mistake is attempting to create an index outside of onupgradeneeded, which is impossible because IndexedDB schema is version-locked. Some candidates also use IDBKeyRange.lowerBound without an upper cap, which would overfetch, or they use a readwrite transaction when readonly is sufficient, reducing concurrency. Finally, forgetting that IndexedDB returns records in index order and suggesting manual sorting shows weak API knowledge.
What usually comes next
The interviewer may ask how to paginate results, which you can achieve by combining openCursor with advance or by using a composite key range. They might ask how to handle a missing index on a legacy client, which requires bumping the database version to trigger onupgradeneeded. Another common follow-up is sorting by price then name; you would create a compound index with keyPath set to an array like [price, name] and query the range on the first element.
A concrete example
Consider a product catalog with one million items. A full store scan would deserialize every record into memory just to discard most of them. By creating a price index and running const range = IDBKeyRange.bound(50, 100); const req = store.index("priceIdx").openCursor(range); the browser reads only the B-tree pages covering that interval, perhaps five hundred records instead of one million. On a low-end mobile device, using the cursor lets you render the first screen of results immediately while the rest stream in, rather than blocking the main thread waiting for a massive array.
Interview question
Which approach correctly and efficiently retrieves IndexedDB products with a price between 50 and 100?
- a.Query the object store primary key with IDBKeyRange.lowerBound and sort the resulting records by price in JavaScript.
- b.Create a price index during onupgradeneeded, then use that index with IDBKeyRange.bound in a readonly transaction.Correct
- c.Open a readwrite transaction and call createIndex on the store to build a temporary price index for the query.
- d.Iterate the entire object store with openCursor and discard records outside the desired price range.
Why? this is the answer
IndexedDB schema is version-locked, so the price index must be created in onupgradeneeded, and a readonly transaction with IDBKeyRange.bound efficiently walks only the relevant B-tree leaf nodes. Option C is impossible because createIndex cannot be used outside onupgradeneeded, while option D triggers a full store scan that deserializes every record.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #indexeddb
- #web-apis
- #browser-storage
- #performance
- #typescript
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles