3

I have an indexedDb that has following indexes

objectStore.createIndex('course', 'course', {unique: false});
objectStore.createIndex('year', 'year', {unique: false});
objectStore.createIndex('session', 'session', {unique: false});
objectStore.createIndex('rollnumber', 'rollnumber', {unique: false});
objectStore.createIndex('name', 'name', {unique: false});

Now I have a form that has fields for all the above indexes. The user fills the form and I have to filter the results according to whatever field user fills. So if the user only fills the course and session field then I have to return all objects that match the two indexes.

Relational version would be simple

SELECT * FROM college WHERE session = ? AND course = ?

I can prepare the query depending on what fields user filled. But in indexedDb I need to know the combination before hand, which is not possible for so many different possible combinations.

For the above case something like this will be required

objectStore.createIndex('courseSession', ['course', 'session'], {unique: false});

Which is all fine and dandy except this is not the only possible combination, user can fill any number of fields it wants to to narrow the result. So how do I accomplish this? I can't write 2^5 (or whatever) possible combinations of those 5 fields.

I found this question which is relevant. But it doesn't solve the problem that I don't know the combination that I will need, so I can't create an index in advance.

TL;DR: I don't know in advance what indexes I want to 'search' the datastore for so I can't create an index.

Community
  • 1
  • 1
Achshar
  • 5,153
  • 8
  • 40
  • 70

1 Answers1

2

SELECT * FROM college WHERE session = ? AND course = ?

That doesn't imply that there is an index on session and course. It'll work even if there is no index. You can do the same in IndexedDB, just search through the entire database for objects matching your constraints.

I can't write 2^5 (or whatever) possible combinations of those 5 fields.

It's actually "5 choose 2" which is 10 possible combinations. So... it's not really that much. You could try adding every possible combination.

Alternative strategy: user inputs 2 (or more) constraints, and you just index on one of them and then manually search over all the results for the other constraints.

dumbmatter
  • 9,351
  • 7
  • 41
  • 80
  • 3
    It is a correct answer, but it would be nicer if it was clearer in some nice way. This might be Achshar's first exposure to the concept. What this answer is pointing out is that you are not using indices in SQL either. But I think what is being asked is really how to quickly query over multiple properties, with or without indices. The answer is that you do not need indices unless you want better performance. Just iterate over all the objects and filter out the ones you don't want. If you want to use an index, learn about query planning in NOSQL. http://en.wikipedia.org/wiki/Query_plan – Josh Jun 06 '14 at 02:15
  • So... what you are basically saying is do the work manually? Like load all the data into an array and filter what I want. That's not exactly what I was expecting. But I can manage I guess. – Achshar Jun 06 '14 at 05:18
  • @Josh You are indeed correct, this is my first time with a NoSQL db. I'll read up more. Thanks! – Achshar Jun 06 '14 at 05:18
  • Yes, the basic simple approach is to iterate. You can iterate per onsuccess callback so you do not need to preload into an array and then iterate over that, but if you avoid the array technique you need to write your per-iteration code in a callback that works as a result of an asynchronous call, which is also more complicated for someone just learning this stuff. Yes, this is bad performance. The better way, and the way indexedDB was intended to be used, was to use indices. But now you run into a very complicated issue in your specific situation regarding how (compound) indices work. – Josh Jun 06 '14 at 12:52
  • See http://stackoverflow.com/questions/23806635 for an explanation of why you will face problems using compound indices in your specific situation. – Josh Jun 06 '14 at 13:19