最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IndexedDB: Query index where value "contains" - Stack Overflow

programmeradmin0浏览0评论

I think the answer is "you can't do it that way", but I wanted to be sure. Let's say I have an object:

var person = {
    name: 'Joe',
    phoneNumbers: [ '5555554455', '4445554455' ]
};

I want to create an index on phoneNumbers:

objectStore.createIndex('phoneNumberIndex', 'phoneNumbers');

Later I want to query for persons with a particular phoneNumber:

index.get('4445554455').onsuccess = function(event) {
    // Anything?
};

Will that produce a result? If not, is there another way to do this?

I think the answer is "you can't do it that way", but I wanted to be sure. Let's say I have an object:

var person = {
    name: 'Joe',
    phoneNumbers: [ '5555554455', '4445554455' ]
};

I want to create an index on phoneNumbers:

objectStore.createIndex('phoneNumberIndex', 'phoneNumbers');

Later I want to query for persons with a particular phoneNumber:

index.get('4445554455').onsuccess = function(event) {
    // Anything?
};

Will that produce a result? If not, is there another way to do this?

Share Improve this question asked Jan 18, 2012 at 18:51 MatthewMatthew 11.6k9 gold badges40 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Something like this should work:

objectStore.createIndex("phoneNumberIndex", "phoneNumber", { multiEntry: true });
objectStore.index("phoneNumberIndex").get("4445554455").onsuccess = function(e) {
  console.log(JSON.stringify(e.target.result));
}

I think you can use the {multientry: true} parameter of createIndex for that.

发布评论

评论列表(0)

  1. 暂无评论