首页 > 解决方案 > 使用 Dexie,我可以获取表中的所有对象,其中数组字段具有特定值作为其元素之一?

问题描述

我有一个表,其中每个对象都有一个字符串数组字段:例如,{ people: ['John', 'Bob', 'Sue'] }. 我需要表中所有在people数组中包含“Sue”的对象。

德西能做到吗?

标签: javascriptarraysfieldindexeddbdexie

解决方案


是的,使用MultiEntry索引您可以做到这一点。

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

async function addRow() {
  await db.groups.add({id: 1, people: ['John', 'Bob', 'Sue']});
}

async function findSuesGroups() (
  return await db.groups.where('people').equals('Sue').toArray();
}

请参阅https://dexie.org/docs/MultiEntry-Index上的其他示例


推荐阅读