gpt4 book ai didi

javascript - 如何在 IndexedDB 中创建具有多个条件的查询

转载 作者:行者123 更新时间:2023-12-05 02:21:58 33 4
gpt4 key购买 nike

我有一个包含多个索引的商店,我想查询。为了这个例子,假设我有一个带有 user_id 索引和 create_date 的消息存储,它是时间戳(并且假设我有索引 - user_id/create_date/user_id, create_date

我知道如何通过 id 查询用户:

var range = IDBKeyRange.only('123');
store.index('user_id').openCursor(range).onsuccess= function(e){....};

而且我知道如何按日期查询:

var range = IDBKeyRange.lowerBound(Date.now()-24 * 60 * 1000))
store.index('create_data').openCursor(range).onsuccess = function(e){....};

购买 我不知道如何一起查询。我知道我可以使用 JS 来解析这两个结果中的任何一个,但我想知道是否可以使用 IDB 来执行此操作。


编辑——我想做的伪查询是

user_id=123 AND create_date < (NOW() - 24 * 60 * 1000)

谢谢!

最佳答案

下面是一个工作示例。

<!DOCTYPE html>
<html lang="en">
<head>
<title>Stackoverflow</title>
<script type="text/javascript" charset="utf-8">
var db_handler = null;
var dbDeleteRequest = window.indexedDB.deleteDatabase("toDoList");
dbDeleteRequest.onerror = function(event) {
console.log("Error while deleting database.", true);
};

dbDeleteRequest.onsuccess = function(event) {
// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 5);

DBOpenRequest.onsuccess = function(event) {
console.log('<li>Database initialised.</li>');

// store the result of opening the database in the db variable. This is used a lot below
db_handler = DBOpenRequest.result;

// Run the addData() function to add the data to the database
addData();
};

DBOpenRequest.onupgradeneeded = function(event) {
console.log('<li>DBOpenRequest.onupgradeneeded</li>');
var db = event.target.result;

db.onerror = function(event) {
console.log('<li>Error loading database.</li>');
};

// Create an objectStore for this database //, { keyPath: "taskTitle" }); { autoIncrement : true }
var objectStore = db.createObjectStore("toDoList", { autoIncrement : true });

// define what data items the objectStore will contain

objectStore.createIndex("user_id", "user_id", { unique: false });
objectStore.createIndex("create_data", "create_data", { unique: false });
objectStore.createIndex("tags",['user_id','create_data'], {unique:false});
};
};

function addData() {
// Create a new object ready to insert into the IDB
var newItem = [];
newItem.push({ user_id: "101", create_data: (1000)});
newItem.push({ user_id: "102", create_data: (Date.now() - 18 * 60 * 1000)});
newItem.push({ user_id: "103", create_data: (Date.now() - 18 * 60 * 1000)});
newItem.push({ user_id: "101", create_data: (2000)});
newItem.push({ user_id: "101", create_data: (989)});
newItem.push({ user_id: "104", create_data: (Date.now() - 18 * 60 * 1000)});

console.log(newItem);

// open a read/write db transaction, ready for adding the data
var transaction = db_handler.transaction(["toDoList"], "readwrite");

// report on the success of opening the transaction
transaction.oncomplete = function(event) {
console.log('<li>Transaction completed: database modification finished.</li>' + new Date());
};


transaction.onerror = function(event) {
console.log('<li>Transaction not opened due to error. Duplicate items not allowed.</li>');
};

// create an object store on the transaction
var objectStore = transaction.objectStore("toDoList");

addData2(transaction, objectStore, newItem, 0, true);

};

function addData2(txn, store, records, i, commitT) {
try {
if (i < records.length) {
var rec = records[i];
var req = store.add(rec);
req.onsuccess = function(ev) {
i++;
console.log("Adding record " + i + " " + new Date());
addData2(txn, store, records, i, commitT);
return;
}
req.onerror = function(ev) {
console.log("Failed to add record." + " Error: " + ev.message);
}
} else if (i == records.length) {
console.log('Finished adding ' + records.length + " records");
}
} catch (e) {
console.log(e.message);
}
//console.log("#########")
};


function select() {
var transaction = db_handler.transaction('toDoList','readonly');
var store = transaction.objectStore('toDoList');
var index = store.index('tags');

var range = IDBKeyRange.bound(['101', 999],['101', 2001]);

var req = index.openCursor(range);

req.onsuccess = function(e){
var cursor = e.target.result;
if (cursor) {
if(cursor.value != null && cursor.value != undefined){
console.log(cursor.value);
}
cursor["continue"]();
}
}
}
</script>
</head>
<body>
<div id="selectButton">
<button onclick="select()">Select Data</button>
<input type="text" id="selectData" value="">
</div>
</body>
</html>

关键点和概念解决你的问题陈述:

  • 要求是从多个属性中搜索值,并进行范围限制搜索。所以,你需要
    • 复杂/复合索引,涵盖您要搜索的属性 (IDBObjectStore.createIndex()),以便您可以搜索更多不止一处。
    • 基于范围的搜索,所以 - IDBKeyRange.bound()

在上面的例子中,

  • 使用 objectStore.createIndex("tags",['user_id','create_data'], {unique:false});
  • 创建复杂或复合索引
  • 范围是使用 var range = IDBKeyRange.bound(['101', 1000],['101', 2000]);
  • 创建的

提醒一句:
var range = IDBKeyRange.bound(['101', 1000],['101', 2000]); 可以很好地满足您的需求,但请确保此 var range 的结果= IDBKeyRange.bound(['101', 1000],['103', 2000]);

如果您使用的是复数/复合范围,则它类似于 101 到 103 OR 1000 到 2000 之间的范围。它不是 AND,而是您指定的复数/复合范围的 OR。

尝试范围的各种组合,您将了解 IDBKeyRange 的全部功能和限制。

关于javascript - 如何在 IndexedDB 中创建具有多个条件的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31403945/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com