Home  >  Q&A  >  body text

javascript - Asking for help with a question about indexedDB

As shown below, the primary key of a piece of data I stored is timeStamp. What I know is that this data can be found by getting the primary key value, but cannot the data be retrieved by querying other values ​​in it? Can query conditions similar to SQL statements not be implemented? (Find them all and then iterate through the results to eliminate them)

db.createObjectStore('book', {
    keyPath: "timeStamp"
});

store.get(1496251171844)

Some people say to use an index, but the index cannot be queried based on the value. I still need to make a judgment. What I
want is to directly store.get(group) to get the specified object storage. group

 var book = db.createObjectStore('book', {
        keyPath: "timeStamp"
    });
  // 建立索引
book.createIndex("groupId","groupId", {unique:false});
--------------------------------------------------------

html5.indexedDB.getFromId = function(groud, callback){
    var db = html5rocks.indexedDB.db;
    var tx = db.transaction(['book'],'readwrite');
    var store = tx.objectStore('book');

  
    var index = store.index("groupId");
    request = index.openCursor();
    request.onsuccess = function(event){
        var result = event.target.result;
        if(!result){return;}
        if(result.value.groupId == groud){
            callback(result.value);
        }
        result.continue();
    };
};
某草草某草草2717 days ago663

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-06-05 11:15:09

    createIndex can add multiple indexes and set whether the index value is unique

    reply
    0
  • 为情所困

    为情所困2017-06-05 11:15:09

    indexedDB is similar to nosql, it can only rely on keyword indexing, and there is no way to query by sql. After obtaining the collection, you can use js method to search.

    reply
    0
  • Cancelreply