温馨提示:本文翻译自stackoverflow.com,查看原文请点击:updates - With Dexie, how to remove a value from an array field for all objects in the table?
dexie indexeddb updates

updates - 使用Dexie,如何从表中所有对象的数组字段中删除值?

发布于 2020-04-25 14:56:41

这个问题中,我可以找到表中所有对象,其中在数组字段中出现一个值。现在,我需要从该字段中删除该表中所有对象的值。

例如,假设一个events带有对象表:

{ people: ['John', 'Bob', 'Sue'] }
{ people: ['Harry', 'Sue', 'Jim'] }
{ people: ['John', 'Bob', 'Elaine'] }
{ people: ['Jim', 'Bob', 'Sue'] }

假设我想从people字段中为所有对象删除“ Sue”

Dexie怎么做?

查看更多

提问者
David Burson
被浏览
74
David Fahlander 2020-02-10 08:02

在异步函数中添加以下代码即可:

await db.events.where('people').equals('Sue').modify(x => {
  // This callback is run for every match.
  // Here you can modify the people property to remove Sue from it:
  x.people = x.people.filter(p => p !== 'Sue');
});

注意:假设架构正在使用multiEntry索引对'people'进行索引:

const db = new Dexie("testdb");
db.version(3).stores({
  events: 'id, *people'
});

参考文献: