Matt Mueller (Prisma Client PM)
3.11.1 patch release. It's a big one for our MongoDB community ๐ฟ prisma rainbow
๐ All of the changes below are specific to the MongoDB Preview. This will be our last release before we announce MongoDB support as production-ready. Please, please, please give these changes a try and tell us know what you think in this issue or jump on a quick call with us. ๐ป
โ Breaking: Filters no longer return undefined fields by default
In preparation for MongoDB General Availability, 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.
๐ช 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 embedded 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 embedded 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,
},
},
},
},
})
๐ Learn more in the release notes
For more info and links to documentation, you can read the release notes.
๐ Help us spread the word about Prisma ๐
To help spread the word about Prisma, weโd very much appreciate it if you would star the repo ๐ And if youโre excited about the features in this weekโs release, then help us and share your excitement on Twitter.