1

Each of my IndexedDB objects have the following format:

object:
{
     objectName: "someName"
     objectTest: "someTest"
     objectAge: "someAge"
}

Then I have the following indexes already set:

storeObject.createIndex( "by_objectName", "objectName", { unique: false } );
storeObject.createIndex( "by_objectTest", "objectTest", { unique: false } );
storeObject.createIndex( "by_objectAge", "objectAge", { unique: false } );

So first I wanted to loop through all my objects by objectName (this is working):

var openedIndex = storeObject.index("by_objectName");
var numItemsInIndex = openedIndex.count();
if (openedIndex) {
    var curCursor = openedIndex.openCursor();               
    curCursor.onsuccess = function(evt) {                   
        var cursor = evt.target.result; 
        if (cursor) {
                //do something
                cursor.continue();
        }
    }
}

So the above code is taking all the objects and they are sorted by objectName. How can I take all the objects that have objectTest: "certainValue" and the sorting by objectName to remain the same as in the above example. I need to filter the list of result before the line if (openedIndex) { because I need to use the numItemsInIndex later in the loop.

So in other words, if this was relational database, how to implement:

SELECT * FROM objects WHERE objectTest = "certainValue" SORT BY objectName
1

1 Answer 1

1

You can create an index on two properties at once by defining the keyPath parameter to createIndex as an array. Use the property you wish to sort by as the first item in the array. For example, see my other posts, like this one: In IndexedDB, is there a way to make a sorted compound query?

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

Comments

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.