Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Prisma - GroupBy relation (e.g "Most used Hashtags")

I would like to show the "Most used Hashtags” in my app.

It's just a simple many-to-many relation:

 model Post {
  id              Int     @id @default(autoincrement())
  title           String  @db.VarChar(255)
  description     String  @db.VarChar(255) 
  hashtags        PostHashtag[] 
}

model PostHashtag { 
  post             Post     @relation(fields: [postId], references: [id])
  postId           Int 
  hashtag          Hashtag @relation(fields: [hashtagId], references: [id])
  hashtagId        Int 

  @@id([postId, hashtagId]) 
  @@unique([postId, hashtagId])
} 
 
model Hashtag {
  id             Int     @id @default(autoincrement())
  name           String @db.VarChar(24)  
  posts          PostHashtag[]
}

I cannot really find a way to groupBy the "name" field of the "Hashtag" in the PostHashtag table.

My goal is a query which returns me the top 15 used hashtags. And unfortunately the docs won't help me with this.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This will return the top 15 used hashtags by ordering with posts counts, with the counts included in the result:

const topHashtags = await prisma.hashtag.findMany({
    take: 15,
    orderBy: {
        posts: { _count: 'desc' },
    },
    include: {
        _count: {
            select: { posts: true },
        },
    },
});
about 4 years ago · Juan Pablo Isaza Report

0

Was easier than I thought:

      return await prisma.hashtag.groupBy({ 
        by: ['name'], 
        _count:{
          name: true
        },
        orderBy: {
          _count: {
            name: 'desc',
          } 
    }
  })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!