Today, we are issuing the 3.11.1
patch release.
MongoDB (Preview)
Breaking: Filters no longer return undefined
fields by default
In 3.11.1
, we've changed what data is returned when filtering MongoDB documents on undefined
fields. The new rule is that undefined
fields are excluded by default unless explicitly filtered for. This allows you to query for undefined and null values separately.
Let's take a look at a concrete example. Given the following Prisma schema:
model Address {
id Int @id @map("_id")
city String
street String? // Note that street is optional
}
For Mongo, optional fields can either be null
or undefined
(absent). The following documents are all valid for the schema above:
{ "_id": 1, "city": "San Fransisco", "street": "Market st." }
{ "_id": 2, "city": "Seattle", "street": null }
{ "_id": 3, "city": "Chicago" }
Prior to 3.11.1
, if you queried for where: { street: null }
, you'd get _id: 2
and _id: 3
. In 3.11.1
, you'll only get _id: 2
. The ability to also query for the missing fields has also been added. For details, refer to the new isSet
below to learn more.
There are a few exceptions to this new default:
- A
having
filter on an aggregated field will returnundefined
fields. This is because aggregation on undefined fields yieldsnull
, notundefined
, thus matching the filter. - Filters on undefined to-many relations (e.g., the backing array of a many-to-many is
undefined
) will currently include those relations in the result set.
New isSet
filter operation
To compensate for missing fields on documents no longer being returned by the filters above, we’ve added a new isSet: bool
filter. This filter can be used to include fields that are undefined
on documents.
Using the example above, to include the undefined
fields, you can use an OR
:
await prisma.address.findMany({
where: {
OR: [
{ street: { isSet: false } },
{ street: null }
]
}
})
The isSet
operation has been added to all scalar and embedded fields that are optional.
New unset
operation
In 3.11.1
, you can also remove a field with the unset
operation.
Using the example above, let's write a query to remove the street field:
await prisma.address.update({
where: {
id: 10,
},
data: {
street: {
unset: true,
},
},
})
This effectively sets the street
field to undefined
in the database.
New updateMany
operation
We now support updating embedded documents that match specific criteria.
For example, given the following schema:
model Product {
id Int @id @map("_id")
name String @unique
photos Photo[]
}
type Photo {
height Int @default(200)
width Int @default(100)
url String
}
Let's update the photo with a url
of 1.jpg
to 2.png
:
const product = prisma.product.update({
where: {
id: 10,
},
data: {
photos: {
updateMany: {
where: {
url: '1.jpg',
},
data: {
url: '2.png',
},
},
},
},
})
New deleteMany
operation
Similar to updateMany
, you can also remove embeds that match specific criteria.
Using the Prisma Schema above, let's delete all photos with a height
of 100:
const product = prisma.product.update({
where: {
id: 10,
},
data: {
photos: {
deleteMany: {
where: {
height: 100,
},
},
},
},
})