如何在indexeddb中搜索字符串

时间:2013-04-14 03:49:23

标签: javascript search indexeddb

我正在尝试在我的网站上设置搜索功能,我正在尝试查找

的IndexedDB代码

SELECT "column" FROM "table" WHERE "column" LIKE "%keyword%"

我在IndexedDB Fuzzy Search

中找到了解决方案
db.transaction(['table'], 'readonly')
    .objectStore('table')
    .openCursor(IDBKeyRange.bound(keyword, keyword + '\uffff'), 'prev')
    .onsuccess = function (e) {
        e || (e = event);
        var cursor = e.target.result;
        if (cursor) {
            console.log(cursor.value.column);
            cursor.continue();
        }
    };

但我怎样才能找到“%keyword%”而不是“%keyword”?

1 个答案:

答案 0 :(得分:2)

与IndexedDB中的SQL WHERE没有任何相似之处。

我会遍历表/对象存储库值并查看当前游标列是否包含关键字:

var keyword = "foo";
var transaction = db.transaction("table", "readwrite");
var objectStore = transaction.objectStore("table");
var request = objectStore.openCursor();
request.onsuccess = function(event) {
    var cursor = event.target.result;
    if (cursor) {
        if (cursor.value.column.indexOf(keyword) !== -1) {                
            console.log("We found a row with value: " + JSON.stringify(cursor.value));
        }  

        cursor.continue();          
    }
};