Be More Productive with MongoDB & Prisma

Bring your developer experience to the next level. Prisma makes it easier than ever to work with your MongoDB database and enables you to query data with confidence.

Why Prisma and MongoDB?

Intuitive Data Modeling

The Prisma schema uses an intuitive modeling language that is easy to read and understand for every team member.

High Productivity & Confidence

Prisma has an intuitive querying API with auto-completion so that you can find the right queries directly in your editor.

Ensured Data Consistency

Prisma’s schema-aware database client ensures that you never bring your data in an inconsistent state.

Fantastic DX

Prisma is well-known for its outstanding developer experience and is loved by developers around the world for it.

First-Class Type-Safety

Prisma provides strong type-safety when used with TypeScript, even for relations and partial queries.

Huge Community & Support

Prisma has a huge Discord community, regularly hosts events and provides helpful support via GitHub.

How Prisma and MongoDB fit together

MongoDB is a powerful NoSQL database that allows developers to intuitively work with their data. However, due to its schemaless nature, developers may run into data inconsistencies as they're evolving their applications. Prisma is a next-generation ORM/ODM that makes it easier to ensure data consistency by providing an easy-to-read schema and a type-safe database client with auto-completion for all queries.

Reading data in MongoDB with Prisma Client

Prisma Client provides a powerful API for reading data in MongoDB, including filters, pagination, ordering and relational queries for embedded documents and reference-based relations.

const usersWithProfile = await prisma.user.findMany({
  where: {
    profile: {
      isSet: true,
    },
  },
  select: {
    id: true,
    profile: {
      select: {
        profilePicture: true,
      },
    },
    posts: {
      where: {
        published: true,
      },
    },
  },
  take: 10,
  orderBy: {
    profile: {
      firstName: "asc",
    },
  },
});

Creating data in MongoDB with Prisma Client

Prisma Client gives you a rich experience when creating data in MongoDB, allowing you to build complex data structures and relations using an intuitive API.

await prisma.post.create({
  data: {
    title: "My New Post",
    author: {
      connectOrCreate: {
        create: {
          email: "sam@prisma.io",
          profile: {
            firstName: "Sam",
            lastName: "Smith",
          },
        },
        where: {
          email: "sam@prisma.io",
        },
      },
    },
  },
});

Updating data in MongoDB with Prisma Client

Prisma Client enables you to update and manage your data in MongoDB with ease, providing a rich set of features allowing you to handle complex updates.

await prisma.user.update({
  where: {
    email: "smith@prisma.io",
  },
  data: {
    email: "harper@prisma.io",
    profile: {
      upsert: {
        update: {
          lastName: "Harper",
          maidenName: "Smith",
        },
        set: {
          firstName: "Sam",
          lastName: "Harper",
        },
      },
    },
  },
});

Deleting data in MongoDB with Prisma Client

Deleting data from your MongoDB is made easy with Prisma Client's API. The Prisma schema additionally allows you to configure referential actions to provide referential integrity in your data as documents are deleted.

await prisma.user.deleteMany({
  where: {
    profile: {
      isSet: false
    }
  },
});

Modeling data in MongoDB with Prisma schema

The Prisma schema allows you to define the shape of the data in your collections, set up relations, and create embedded documents within your collections in a way that is easy to understand.

model User {
  id        String   @id @default(auto()) @map("_id") @db.ObjectId
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  email     String?  @unique
  posts     Post[]
  profile   Profile?
}

model Post {
  id        String  @id @default(auto()) @map("_id") @db.ObjectId
  title     String
  content   String?
  published Boolean @default(false)
  author    User    @relation(references: [id], fields: [authorId], onDelete: Cascade)
  authorId  String  @db.ObjectId
}

type Profile {
  firstName      String
  lastName       String
  maidenName     String?
  profilePicture String?
  department     Department?
}

enum Department {
  Marketing
  Sales
  Engineering
}

We believe that the combination of MongoDB Atlas Serverless and Prisma Accelerate will greatly simplify the process of building and deploying serverless applications in the cloud, especially for workloads that need to scale to high connection counts.

Kevin Jernigan
Kevin Jernigan
Principal Product Manager
MongoDB

Our MongoDB Resources

Using Prisma with MongoDB

In this guide, you will learn about the concepts behind using Prisma and MongoDB, the commonalities and differences between MongoDB and other database providers, and the process for configuring your application to integrate with MongoDB using Prisma.

MongoDB in the Data Guide

Learn how to use MongoDB to its fullest to take advantage of the performance and features that developers have grown to rely on.

Watch

In this episode of What's New in Prisma, Matt takes you through a demo of the embedded document support in MongoDB.