Вопрос

Apologies if my terminology is incorrect or this isn't well explained. I created something a while back with a websql database and would like to try changing it to an indexed DB.

I've created this index which allows me to find the objects where the hometeam is a certain value and the away team is a certain value:

objectStore.createIndex("homeAway", ["hometeam", "awayteam"], {unique: false});

So if I'm looking for a match between USA (hometeam) and Russia (awayteam) I'd get what I'm after:

var range = IDBKeyRange.only([hometeam, awayteam]);
var index = objectStore.index("homeAway");

But what if I wanted to use this .only method to find a match where a certain team is playing home OR away. Something like this:

var range = IDBKeyRange.only([hometeam || awayteam]);

Where if I entered Russia as both home and away it would find where they were either home or away. Is this possible?

Это было полезно?

Решение

Opening and index on two fields at once it's not possible however there is a indexed solution for your case.

You need to create a new index for an array field which will contain both teams with multiEntry property set to true.

objectStore.createIndex("teams", ["teams"], {unique: false, multiEntry: true});

And then store both teams as array of strings into that field

var obj = {
    hometeam: "Russia",
    awayteam: "Usa",
    teams: ["Russia", "Usa"]
}

Now when you query the database on that field you should get all games in which Russia is playing:

var index = objectStore.index("teams");
var keyRange = IDBKeyRange.only("Russia");
var cursorRequest = index.openCursor(keyRange);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top