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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.