9

I have an object,

var _data = {
        teacher : {
            name : "Mack",
            subject : "Maths"
        },
        Student : [{
            name : "Alex",
            stud_id : 1
        },{
            name : "Jack",
            stud_id : 2
        },{
            name : "Mark",
            stud_id : 3
        }]

}

I want to create index on stud_id, how can I create?

User 780611
  • 175
  • 1
  • 12

2 Answers2

4

What I would do that is to have two separate object stores inside of your database, one which represents teachers and one which represents students.

Then I'd store a teacher object like:

{
    name: 'John Smith',
    subject: 'Maths',
    students: [1,2,3,4,5]
}

Then I'd have a store for students like:

{
    id: 1,
    name: 'Jane Doe'
}

I'd then have an id key path in both stores (autogenerated or not, depending on whether it syncs somewhere) so you have a unique identifier of what the data is.

Finally you can create an index on the students property of a teacher and set the multiEntry flag to true so that it is known to be an array and you can query the object store on it as an array, see here for more info on multiEntry indexes.

Note: At time of writing IE10 & IE11 do not support multiEntry indexes in IndexedDB.

Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
0

Currently it is not possible. Element of keyPath must be string.

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • If I want to apply the index on array element which is string. In the object above Student array has name property, can I apply index on name, and how? – User 780611 Jan 03 '14 at 10:18