Query using multiple conditions Query using multiple conditions javascript javascript

Query using multiple conditions


Check out this answer to the same question. It is more detailed than the answer I give here. The keypath parameter to store.createIndex and IDBKeyRange methods can be an array. So, crude example:

// In onupgradeneededvar store = db.createObjectStore('mystore');store.createIndex('myindex', ['prop1','prop2'], {unique:false});// In your query sectionvar transaction = db.transaction('mystore','readonly');var store = transaction.objectStore('mystore');var index = store.index('myindex');// Select only those records where prop1=value1 and prop2=value2var request = index.openCursor(IDBKeyRange.only([value1, value2]));// Select the first matching recordvar request = index.get(IDBKeyRange.only([value1, value2]));


Let's say your SQL Query is something like:

SELECT * FROM TableName WHERE Column1 = 'value1' AND Column2 = 'value2'

Equivalent Query in JsStore library:

var Connection = new JsStore.Instance("YourDbName");Connection.select({    From: "YourTableName"    Where: {        Column1: 'value1',        Column2: 'value2'    },    OnSuccess:function (results){        console.log(results);    },    OnError:function (error) {        console.log(error);    }});

Now, if you are wondering what JsStore is, let me tell you it is a library to query IndexedDB in a simplified manner. Click here to learn more about JsStore


I mention some suggestions for querying relationships in my answer to this question, which may be of interest:

Conceptual problems with IndexedDB (relationships etc.)

As to querying multiple fields at once, it doesn't look like there's a native way to do that in IndexedDB (I could be wrong; I'm still new to it), but you could certainly create a helper function that used a separate cursor for each field, and iterated over them to see which records met all the criteria.