Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda