Welcome to the MongoDB Early Access Program. This guide will help you get up and running with MongoDB on Prisma. In the following guide, we'll walk you through setting up a basic blog that runs on Prisma and MongoDB.

Please note that MongoDB with Prisma is not yet production-ready. We wanted to get this code in your hands as early as possible, but there will be bugs and limitations.

If you have any feedback or run into issues, we're here for you.

Prerequisites

A brief detour on what you'll need to try this:

Setting up our Project

Let's create the backend for a basic blog. To get setup the project, run the following in your Terminal:

# Create a new blog
mkdir blog
cd blog

# Initialize our package.json
npm init -y

# Install our development dependencies
npm install -D prisma typescript ts-node @types/node

# Initialize the Prisma Schema
npx prisma init

# Open the project in the editor of your choice
# For example, in VSCode.
code .

This will set you up with a fresh Prisma project. If you'd like to try MongoDB on an existing Prisma project, you'll need to make sure you're running Prisma 2.21.2 and above.

Creating our Prisma Schema

First we'll define the schema for our blog. We'll start with posts.

Open the prisma/schema.prisma file and replace that default contents with the following:

// We want to connect to the MongoDB datasource
datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

// We want to generate a typescript client
// Since mongodb is a preview feature, we'll enable that.
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["mongodb"]
}

// Create our post schema. All posts have a title, slug and body.
// The id attributes tell Prisma it's a primary key and to generate 
// object ids by default when we insert posts.
model Post {
  id    String @id @default(auto()) @map("_id") @db.ObjectId
  slug  String @unique
  title String
  body  String
}

The Prisma Schema serves as the structure of your application data. We use this file to generate application-specific type-safe database clients.