Skip to main content

Mongoose

本页比较 Prisma ORM 和 Mongoose API。如果你想了解如何从 Mongoose 迁移到 Prisma,请查看此 guide

¥This page compares the Prisma ORM and Mongoose APIs. If you want to learn how to migrate from Mongoose to Prisma, check out this guide.

获取单个对象

¥Fetching single objects

Prisma ORM

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

Mongoose

const result = await User.findById(1)

获取单个对象的选定标量

¥Fetching selected scalars of single objects

Prisma ORM

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

Mongoose

const user = await User.findById(1).select(['name'])

获取关系

¥Fetching relations

Prisma ORM

const userWithPost = await prisma.user.findUnique({
where: {
id: 2,
},
include: {
post: true,
},
})

Mongoose

const userWithPost = await User.findById(2).populate('post')

过滤具体值

¥Filtering for concrete values

Prisma ORM

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

Mongoose

const posts = await Post.find({
title: 'Hello World',
})

其他过滤条件

¥Other filter criteria

Prisma ORM

Prisma ORM 生成许多现代应用开发中常用的 附加过滤器

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

Mongoose

Mongoose 将 MongoDB 查询选择器 公开为过滤条件。

¥Mongoose exposes the MongoDB query selectors as filter criteria.

关系过滤器

¥Relation filters

Prisma ORM

Prisma ORM 允许你根据条件过滤列表,该条件不仅适用于正在检索的列表的模型,还适用于该模型的关系。

¥Prisma ORM 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 不提供关系过滤器的专用 API。你可以通过添加额外的步骤来过滤查询返回的结果来获得类似的功能。

¥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 ORM

光标式分页:

¥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: 200,
limit: 20,
})

创建对象

¥Creating objects

Prisma ORM

const user = await prisma.user.create({
data: {
name: 'Alice',
email: 'alice@prisma.io',
},
})

Mongoose

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

更新对象

¥Updating objects

Prisma ORM

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

Mongoose

const updatedUser = await User.findOneAndUpdate(
{ _id: 2 },
{
$set: {
name: 'Alicia',
},
}
)

删除对象

¥Deleting objects

Prisma ORM

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

Mongoose

await User.deleteOne({ _id: 10 })

批量删除

¥Batch deletes

Prisma ORM

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

Mongoose

await User.deleteMany({ id: { $in: [1, 2, 6, 6, 22, 21, 25] } })

Stay connected with Prisma

Continue your Prisma journey by connecting with our active community. Stay informed, get involved, and collaborate with other developers:

We genuinely value your involvement and look forward to having you as part of our community!