Overview

This page compares the Prisma and Mongoose APIs.

Note: Prisma does not currently support MongoDB.

Fetching single objects

Prisma

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

Mongoose

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

Fetching selected scalars of single objects

Prisma

1const user = await prisma.user.findOne({
2 where: {
3 id: 1,
4 },
5 select: {
6 name: true,
7 },
8});

Mongoose

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

Fetching relations

Prisma

Using include
Fluent API
1const posts = await prisma.user.findOne({
2 where: {
3 id: 2,
4 },
5 include: {
6 post: true,
7 },
8});

Mongoose

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

Filtering for concrete values

Prisma

1const posts = await prisma.post.findMany({
2 where: {
3 title: {
4 contains: "Hello World",
5 },
6 },
7});

Mongoose

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

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:

1const posts = await prisma.user.findMany({
2 where: {
3 Post: {
4 some: {
5 title: {
6 contains: "Hello",
7 },
8 },
9 },
10 }
11});

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:

1const page = prisma.post.findMany({
2 before: {
3 id: 242,
4 },
5 last: 20,
6});

Offset pagination:

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

Mongoose

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

Creating objects

Prisma

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

Mongoose

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

Updating objects

Prisma

1const user = await prisma.user.update({
2 data: {
3 name: "Alicia",
4 },
5 where: {
6 id: 2
7 }
8});

Mongoose

Using `findOneAndUpdate`
Using `save`
1const updatedUser = await User.findOneAndUpdate(
2 { _id: id },
3 {
4 $set: {
5 name: "James",
6 email: "james@prisma.io"
7 }
8 }
9)

Deleting objects

Prisma

1const user = prisma.user.delete({
2 where: {
3 id: 10,
4 },
5});

Mongoose

1await User.deleteOne({ _id: id })

Batch deletes

Prisma

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

Mongoose

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