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

186
Views
Prisma order posts based on likes

I have a Post model:

model Post {
  id      String @id @default(auto()) @map("_id") @db.ObjectId
  title   String
  content String
  likes   Like[]
}

And a Like model:

model Like {
  id        String   @id @default(auto()) @map("_id") @db.ObjectId
  user      User     @relation(fields: [userId], references: [id])
  userId    String   @db.ObjectId
  postId    String   @db.ObjectId
  post      Post     @relation(fields: [postId], references: [id])
  likeType  LikeType
}

enum LikeType {
  like
  dislike
}

I want to order posts based on the number of Likes (not dislikes) a post receives. Something like:

prisma.post.findMany({
    orderBy: {
      likes: {
        _count: {
          where: {
            likeType: "like",
          }
        }
      }
    }
  })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can sort by a relation from 2.19.0 by aggregates including count. But it's currently not possible to return the count of a relation itself. Separating the models for likes and dislikes simplifies the query pattern.

const orderedPosts = await prisma.post.findMany({
  orderBy: {
    likes: {
      count: 'asc'
    }
  }
})
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!