Overview

This page compares the Prisma and Mongoose APIs.

Note: Prisma does not currently support MongoDB.

Fetching single objects

Prisma

const user = await prisma.user.findOne({
where: {
id: 1,
},
});

Mongoose

const result = await User.findById("5eb9354624286a04e42401d8");

Fetching selected scalars of single objects

Prisma

const user = await prisma.user.findOne({
where: {
id: 1,
},
select: {
name: true,
},
});

Mongoose

const user = await User.findById("5eb9354624286a04e42401d8").select([
"name",
"email",
]);

Fetching relations

Prisma

Using include
Fluent API
const posts = await prisma.user.findOne({
where: {
id: 2,
},
include: {
post: true,
},
});

Mongoose

const userWithPosts = await User
.findById(id)
.populate("posts")

Filtering for concrete values

Prisma

const posts = await prisma.post.findMany({
where: {
title: {
contains: "Hello World",
},
},
});

Mongoose

const user = await Post.find({
title: "Hello World"
})

Other filter criteria

Prisma

Prisma generates many additional filters that are commonly used in modern application development.

Mongoose

Mongoose exposes the MongoDB query selectors as filter criteria.

Relation filters

Prisma

Prisma lets you filter a list based on a criteria that applies not only to the models of the list being retrieved, but to a relation of that model.

For example, the following query returns users with one or more posts with "Hello" in the title:

const posts = await prisma.user.findMany({
where: {
Post: {
some: {
title: {
contains: "Hello",
},
},
},
}
});

Mongoose

Mongoose doesn't offer a dedicated API for relation filters. You can get similar functionality by adding an additional step to filter the results returned by the query.

Pagination

Prisma

Cursor-style pagination:

const page = prisma.post.findMany({
before: {
id: 242,
},
last: 20,
});

Offset pagination:

const cc = prisma.post.findMany({
skip: 200,
first: 20,
});

Mongoose

const posts = await Post.find({
skip: 5,
limit: 10
})

Creating objects

Prisma

const user = await prisma.user.create({
data: {
email: "alice@prisma.io",
},
});

Mongoose

Using `create`
Using `save`
const user = await User.create({
name: "Alice",
email: "alice@prisma.io"
})

Updating objects

Prisma

const user = await prisma.user.update({
data: {
name: "Alicia",
},
where: {
id: 2
}
});

Mongoose

Using `findOneAndUpdate`
Using `save`
const updatedUser = await User.findOneAndUpdate(
{ _id: id },
{
$set: {
name: "James",
email: "james@prisma.io"
}
}
)

Deleting objects

Prisma

const user = prisma.user.delete({
where: {
id: 10,
},
});

Mongoose

await User.deleteOne({ _id: id })

Batch deletes

Prisma

const users = await prisma.user.deleteMany({
where: {
id: {
in: [1, 2, 6, 6, 22, 21, 25],
},
},
});

Mongoose

await User.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5]}})
Edit this page on Github