Back to Prisma1

Prisma Vs Mongoose Ys8c

docs/1.34/understand-prisma/prisma-vs-traditional-orms/prisma-vs-mongoose-ys8c.mdx

1.34.127.8 KB
Original Source

import Code from "components/Markdown/Code" import Icon from 'components/Markdown/Icon' import Info from 'components/Markdown/Info'

export const meta = { title: "Prisma & Mongoose", position: 80, description: "Learn how Prisma 1 compares to Mongoose" }

Overview

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

This page compares Prisma with Mongoose. Here is a high-level overview:

PrismaMongoose
Auto-generated DB client<Icon type="check" /><Icon type="cross" />
Type-safe<Icon type="check" /><Icon type="cross" />
Declarative data modelling & migrations<Icon type="check" /><Icon type="cross" />
Connection pooling<Icon type="check" /><Icon type="check" />
Supported DB typesDocument & RelationalDocument
Supported ORM patternsDataMapperActive Record
Fluent API for relations<Icon type="check" /><Icon type="cross" />
Relation filters<Icon type="check" /><Icon type="cross" />

In the following, you find a detailled comparison of the Mongoose and Prisma APIs.

Fetching single objects

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

ts
const user = await prisma.user({ id })

Mongoose

ts
const user = await findById(id)

Fetching selected scalars of single objects

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

ts
const userFragment = await prisma.user({ id }).$fragment(`
  fragment NameAndEmail on User { id email }`
`)

Mongoose

ts
const user = await findById(id)
  .select("id email")

Fetching relations

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

<Code hideCopy={true} languages={["Fluent API", "Using fragments", "Native GraphQL"]}>

ts
const postsByUser = await prisma
  .user({ id })
  .posts() 
ts
const userWithPosts = await prisma
  .user({ id })
  .$fragment(`
    fragment UserPosts on User {
      posts { id title content published }
    }
  `)
ts
const userWithPosts = await prisma
  .$graphql(`
    query ($id: ID!) {
      user(id: $id) {
        posts { id title content published }
      }
    }
  `, 
  { id }
  )
</Code>

While the $fragment and the $graphql APIs each return a user object that includes a posts array, the fluent API returns just the posts array and no data about the user.

See the following GitHub issues to learn more about planned iterations of the Prisma client relations API:

Mongoose

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

Filtering for concrete values

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

ts
const users = await prisma.users({
  where: {
    name: "Alice"
  }
})

Mongoose

ts
const user = await User.find({
  name: "Alice"
})

Other filter criteria

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

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

  • <field>_ends_with & <field>_starts_with
  • <field>_not_ends_with & <field>_not_starts_with
  • <field>_gt & <field>_gte
  • <field>_lt & <field>_lte
  • <field>_contains & <field>_not_contains
  • <field>_in & <field>_not_in

Mongoose

Mongoose exposes the MongoDB query selectors as filter criteria.

Relation filters

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

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, you want to fetch only those users that wrote a post with the title "Hello World":

ts
query {
  user(where: {
    posts_some: {
      title: "Hello World"
    }
  }) {
    id
  }
}

Mongoose

Mongoose doesn't offer a dedicated API for relation filters. You can get similar functionality by sending a raw SQL query to the database.

Pagination

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

ts
const posts = await prisma.posts({
  skip: 5,
  first: 10
})

In addition to skip and first, the Prisma API also offers:

  • last
  • before & after for cursor based pagination
  • Relay-style pagination

Mongoose

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

Creating objects

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

ts
const user = await prisma.createUser({
  name: "Alice",
  email: "[email protected]"
})

Mongoose

<Code hideCopy={true} languages={["Using create", "Using save"]}>

ts
const user = await User.create({
  name: "Alice",
  email: "[email protected]"
})
ts
const user = new User({
  name: "Alice",
  email: "[email protected]"
})
await user.save()
</Code>

Updating objects

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

ts
const updatedUser = await prisma.updateUser({
  where: { id },
  data: {
    name: "James",
    email: "[email protected]"
  }
})

Mongoose

<Code hideCopy={true} languages={["Using findOneAndUpdate", "Using save"]}>

ts
const updatedUser = await User.findOneAndUpdate(
  { _id: id },
  {
    $set: {
      name: "James",
      email: "[email protected]"
    }
  }
)
ts
user.name = "James"
user.email =" [email protected]"
await user.save()
</Code>

Deleting objects

<Info>

This page is outdated! It compares Prisma 1 with Mongoose. You can find an updated version comparing Mongoose with Prisma 2 here.

</Info>

Prisma

ts
const deletedUser = await prisma.deleteUser({ id })

Mongoose

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