问题
I have tried following the tutorial at http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/ with regards to doing queries in IndexedDB, but their example does not work.
How to I do a JOIN type query in IndexedDB? I have set my objectstores up with indexes, but I just cant seem to get the syntax?
回答1:
IndexedDB is key-value (document) store. It doesn't have JOIN query or querying over multiple object store. However you can query multiple stores in a transaction. That is how suppose to make join query in IndexedDB.
I have a bit of writeup modeling relationship http://dev.yathit.com/ydn-db/schema.html using my library.
Here is joining query for SELECT * FROM Supplier, Part WHERE Supplier.CITY = Part.CITY
.
var iter_supplier = new ydn.db.IndexValueIterator('Supplier', 'CITY');
var iter_part = new ydn.db.IndexValueIterator('Part', 'CITY');
var req = db.scan(function(keys, values) {
var SID = keys[0];
var PID = keys[1];
console.log(SID, PID);
if (!SID || !PID) {
return []; // done
}
var cmp = ydn.db.cmp(SID, PID); // compare keys
if (cmp == 0) {
console.log(values[0], values[1]);
return [true, true]; // advance both
} else if (cmp == 1) {
return [undefined, SID]; // jump PID cursor to match SID
} else {
return [PID, undefined]; // jump SID cursor to match PID
}
}, [iter_supplier, iter_part]);
See more detail on Join query article.
来源:https://stackoverflow.com/questions/16547954/how-do-i-do-an-join-type-query-in-indexeddb