使用标量列表
标量列表 由 [] 修饰符表示,并且仅在底层数据库支持标量列表时才可用。以下示例有一个名为 pets 的标量 String 列表:
¥Scalar lists are represented by the [] modifier and are only available if the underlying database supports scalar lists. The following example has one scalar String list named pets:
- Relational databases
- MongoDB
model User {
id Int @id @default(autoincrement())
name String
pets String[]
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
pets String[]
}
字段值示例:
¥Example field value:
['Fido', 'Snoopy', 'Brian']
设置标量列表的值
¥Setting the value of a scalar list
以下示例演示了在创建模型时如何对标量列表 (coinflips) 的值进行 set:
¥The following example demonstrates how to set the value of a scalar list (coinflips) when you create a model:
const createdUser = await prisma.user.create({
data: {
email: 'eloise@prisma.io',
coinflips: [true, true, true, false, true],
},
})
取消设置标量列表的值
¥Unsetting the value of a scalar list
以下示例演示如何对标量列表的值 (coinflips) 进行 unset:
¥The following example demonstrates how to unset the value of a scalar list (coinflips):
const createdUser = await prisma.user.create({
data: {
email: 'eloise@prisma.io',
coinflips: {
unset: true,
},
},
})
与 set: null 不同,unset 完全删除了该列表。
¥Unlike set: null, unset removes the list entirely.
将项目添加到标量列表
¥Adding items to a scalar list
使用 push 方法将单个值添加到标量列表:
¥Use the push method to add a single value to a scalar list:
const userUpdate = await prisma.user.update({
where: {
id: 9,
},
data: {
coinflips: {
push: true,
},
},
})
在早期版本中,你必须覆盖整个值。以下示例检索用户,使用 push() 添加三个新的硬币翻转,并覆盖 update 中的 coinflips 字段:
¥In earlier versions, you have to overwrite the entire value. The following example retrieves user, uses push() to add three new coin flips, and overwrites the coinflips field in an update:
const user = await prisma.user.findUnique({
where: {
email: 'eloise@prisma.io',
},
})
if (user) {
console.log(user.coinflips)
user.coinflips.push(true, true, false)
const updatedUser = await prisma.user.update({
where: {
email: 'eloise@prisma.io',
},
data: {
coinflips: user.coinflips,
},
})
console.log(updatedUser.coinflips)
}
过滤标量列表
¥Filtering scalar lists
使用 标量列表过滤器 筛选具有与特定条件匹配的标量列表的记录。以下示例返回标签列表包含 databases 和 typescript 的所有帖子:
¥Use scalar list filters to filter for records with scalar lists that match a specific condition. The following example returns all posts where the tags list includes databases and typescript:
const posts = await prisma.post.findMany({
where: {
tags: {
hasEvery: ['databases', 'typescript'],
},
},
})
数组中的 NULL 值
¥NULL values in arrays
将标量列表过滤器与关系数据库连接器一起使用时,以下条件不考虑具有 NULL 值的数组字段:
¥When using scalar list filters with a relational database connector, array fields with a NULL value are not considered by the following conditions:
-
NOT(数组不包含 X)¥
NOT(array does not contain X) -
isEmpty(数组为空)¥
isEmpty(array is empty)
这意味着你可能期望看到的记录不会返回。考虑以下示例:
¥This means that records you might expect to see are not returned. Consider the following examples:
-
以下查询返回
tags不包含databases的所有帖子:¥The following query returns all posts where the
tagsdo not includedatabases:const posts = await prisma.post.findMany({
where: {
NOT: {
tags: {
has: 'databases',
},
},
},
})-
✔ 不包含
"databases"的数组,例如{"typescript", "graphql"}¥✔ Arrays that do not contain
"databases", such as{"typescript", "graphql"} -
✔ 空数组,例如
[]¥✔ Empty arrays, such as
[]
查询不返回:
¥The query does not return:
-
✘
NULL数组,即使它们不包含"databases"¥✘
NULLarrays, even though they do not contain"databases"
-
以下查询返回 tags 为空的所有帖子:
¥The following query returns all posts where tags is empty:
const posts = await prisma.post.findMany({
where: {
tags: {
isEmpty: true,
},
},
})
查询返回:
¥The query returns:
-
✔ 空数组,例如
[]¥✔ Empty arrays, such as
[]
查询不返回:
¥The query does not return:
-
✘
NULL数组,即使它们可以被视为空¥✘
NULLarrays, even though they could be considered empty
要解决此问题,你可以将数组字段的默认值设置为 []。
¥To work around this issue, you can set the default value of array fields to [].