我如何对Prisma中的某个条款进行计数?

发布于 2025-02-03 02:14:01 字数 211 浏览 2 评论 0 原文

我有以下查询,其中提供了所有帖子和所有评论的数量。现在,我想在帖子中获得所有评论,这些帖子将批准的字段设置为true。我似乎无法弄清楚。

prisma.post.findMany({
    include: {
      _count: { select: { Comment: true } },
    },
  });

感谢您的帮助。

I have the following query which gives all posts and a count of all comments. Now I'd like to get a count of all comments with the post that have the approved field set to true. I can't seem to figure this out.

prisma.post.findMany({
    include: {
      _count: { select: { Comment: true } },
    },
  });

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断念 2025-02-10 02:14:01

4.3.0

在模式文件中启用:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["filteredRelationCount"] << add this 
}

然后查询:

await prisma.post.findMany({
  select: {
    _count: {
      select: {
        comment: { where: { approved: true } },
      },
    },
  },
})

Available since 4.3.0.

enable in your schema file:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["filteredRelationCount"] << add this 
}

and then query:

await prisma.post.findMany({
  select: {
    _count: {
      select: {
        comment: { where: { approved: true } },
      },
    },
  },
})
绅士风度i 2025-02-10 02:14:01

您需要使用 _Count上的过滤器 in Resption>尚不支持将其实现。

这是相同的功能请求:能够过滤“计数关系”中的计数

[Edit: 14-Nov-2022]

Prisma has added support for filteredRelationCount since version

You would need to use Raw Query to achieve this as the filter on _count for relations is not supported yet.

Here's the Feature Request for the same: Ability to filter count in "Count Relation Feature"

[Edit: 14-Nov-2022]

Prisma has added support for filteredRelationCount since version 4.3.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文