简体   繁体   English

如何使用 where 子句在 prisma 中包含 model 数据计数?

[英]How to get included model data count in prisma with where clause?

i need to get user details with post count(Number of posts for today).我需要获取带有帖子数的用户详细信息(今天的帖子数)。

const usersWithCount = await prisma.user.findMany({
      select: {
        _count: {
          select: {
            posts: {
             where: {
                       createdAt: moment().format("YYYY-MM-DD"),
                    },
                  },
            recipes: true,
          },
        },
      },
    })

You cannot filter in relations count, this is not currently supported by prisma.您无法过滤关系计数,prisma 目前不支持此功能。 Here is the Feature Request for adding filters in relations count.这是在关系计数中添加过滤器的功能请求 In your use case you can get filtered relations as described below:在您的用例中,您可以获得如下所述的过滤关系:

  const usersWithCount = await prisma.user.findMany({
    select: {
      posts: {
        where: {
          createdAt: moment().format("YYYY-MM-DD"),
        },
      },
      recipes: true
    },
  });

In the response of the above query you will get posts array with records satisfying your where condition, you can use the array's length as a count.在上述查询的响应中,您将获得包含满足您的 where 条件的记录的 posts 数组,您可以使用数组的长度作为计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM