从indexeddb索引获取唯一ID

时间:2014-04-01 10:08:55

标签: indexeddb

在索引数据库中,我有以下记录

Key               Value
emp_1             { emp_fName : "Mike",
                    emp_lName : "Davis",
                    dept_id   : 1
                  }

emp_2             { emp_fName : "Merry",
                    emp_lName : "Bond",
                    dept_id   : 1
                  }

我在dept_id上创建了索引,我想获取唯一的dept_id,我该怎么做?

1 个答案:

答案 0 :(得分:2)

您希望在IDBIndex对象上使用openCursor()openKeyCursor()在索引上创建cursor。来自the spec的示例:

var tx = db.transaction("books", "readonly");
var store = tx.objectStore("books");
var index = store.index("by_author");

var request = index.openCursor(IDBKeyRange.only("Fred"));
request.onsuccess = function() {
  var cursor = request.result;
  if (cursor) {
    // Called for each matching record.
    report(cursor.value.isbn, cursor.value.title, cursor.value.author);
    cursor.continue();
  } else {
    // No more matching records.
    report(null);
  }
};

您希望设置为IDBCursorDirection的特定标记为nextunique(相对于默认值next),其中"不应生成记录相同的密钥,但以其他方式产生所有记录,以单调递增的密钥顺序。"